Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<?xml version="1.0" encoding="utf-8"?>
<DataDownload>
  <ClusterId id="00 15">
    <attrib>00 00</attrib>
    <attrib>01 00</attrib>
    <attrib>02 00</attrib>
    <attrib>03 00</attrib>
  </ClusterId>
  <ClusterId id="00 00">
    <attrib>00 00</attrib>
    <attrib>04 00</attrib>
    <attrib>05 00</attrib>
    <attrib>07 00</attrib>
  </ClusterId>
  <ClusterId id="07 02">
   <attrib>00 00</attrib>
    <attrib>00 02</attrib>
    <attrib>00 03</attrib>
    <attrib>03 03</attrib>
    <attrib>06 03</attrib>
  </ClusterId>
</DataDownload >


This is my xml file
i want to retieve attrib values based on cluster ID node id.

example if 00 15 id means
its childnodes values i need.
Posted

1 solution

You can use a XPath query to locate the element based on the id. For example something like:
C#
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

         xmlDoc.LoadXml(@"
<DataDownload>
  <ClusterId id=""00 15"">
    <attrib>00 00</attrib>
    <attrib>01 00</attrib>
    <attrib>02 00</attrib>
    <attrib>03 00</attrib>
  </ClusterId>
  <ClusterId id=""00 00"">
    <attrib>00 00</attrib>
    <attrib>04 00</attrib>
    <attrib>05 00</attrib>
    <attrib>07 00</attrib>
  </ClusterId>
  <ClusterId id=""07 02"">
   <attrib>00 00</attrib>
    <attrib>00 02</attrib>
    <attrib>00 03</attrib>
    <attrib>03 03</attrib>
    <attrib>06 03</attrib>
  </ClusterId>
</DataDownload >");

         System.Xml.XmlNodeList result = xmlDoc.SelectNodes("//ClusterId[@id='00 15']");

         if (result.Count > 0) {
            // Here you go through the child nodes of the first result (result[0])
         }
 
Share this answer
 
Comments
Espen Harlinn 1-Sep-12 17:14pm    
5'ed!
Wendelius 2-Sep-12 2:13am    
Thanks Espen :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900