Jul 23, 2012

How To read XML file using Xpath


XmlDocument doc = new XmlDocument();
            doc.Load(strNonMSCatalog + "\\Masterreport.xml");
         
            DataTable dataTable = new DataTable("MyReport");
            dataTable.Columns.Add("Name", typeof(string));
            dataTable.Columns.Add("BulletinID", typeof(string));
            dataTable.Columns.Add("Title", typeof(string));
            dataTable.Columns.Add("IsInstalled", typeof(string));
           
         
            XmlNodeList nodeListSysName = doc.SelectNodes("/MasterReport/SystemName");

            foreach (XmlNode sysNameNode in nodeListSysName)
            {
                if (sysNameNode.HasChildNodes)
                {

                    string xpathQueryForSysName = "/MasterReport/SystemName[@Name='" + sysNameNode.Attributes["Name"].Value + "']/ScanReport/Microsoft/Update";
                    XmlNodeList nodeListUpdate = sysNameNode.SelectNodes(xpathQueryForSysName);
                    foreach (XmlNode innernode in nodeListUpdate)
                    {
                        dataTable.Rows.Add(sysNameNode.Attributes["Name"].Value, innernode.Attributes["BulletinID"].Value, innernode.Attributes["Title"].Value, innernode.Attributes["IsInstalled"].Value);
                    }
                }
            }

Xml File:



- <MasterReport>
  <SystemName Name="ASCINDAE745131" Action="No Missing Patches" />
- <SystemName Name="WIN7-PC">
- <ScanReport>
- <Microsoft>
  <Update BulletinID="12" Title="Security" IsInstalled="true" />
  <Update BulletinID="56" Title="Security 3" IsInstalled="false" />
  <Update BulletinID="90" Title="Security  7 SP0" IsInstalled="false" />
  <Update BulletinID="34" Title="Security  R2" IsInstalled="false" />
  <Update BulletinID="78" Title="Security 3" IsInstalled="false" />
  <Update BulletinID="01" Title="Security 8" IsInstalled="false" />
  </Microsoft>
  </ScanReport>
  </SystemName>
- <SystemName Name="WINXP-PC">
- <ScanReport>
- <Microsoft>
  <Update BulletinID="12" Title="Security " IsInstalled="false" />
  <Update BulletinID="56" Title="Security 3" IsInstalled="false" />
  <Update BulletinID="90" Title="Security 0" IsInstalled="false" />
  <Update BulletinID="34" Title="Security R2" IsInstalled="false" />
  <Update BulletinID="78" Title="Security3" IsInstalled="false" />
  <Update BulletinID="01" Title="Security  8" IsInstalled="false" />
  </Microsoft>
  </ScanReport>
  </SystemName>
  </MasterReport>

No comments:

Post a Comment