Piper
Piper

Reputation: 55

How do I set a string to an attribute value in an xml file?

I'll do my best to make this as clear as I can.

XmlNodeList ZONE = nodRoot.SelectNodes("CATALOG/PLANTS/ZONE");

This allows me to search for Plants in Zone 4. When I find one, I want to be able to get the ID # from the attribute above. I can't figure out how to get this. I've tried

I have code doing this.

string ID = null;
 foreach(XmlNode xmlNodeComplex in ZONE)
      {
        if(xmlNodeComplex.InnerText == "4")
            {
             ID = xmlNodeComplex.ParentNode.InnerText;
             .....
            }
       ....
      }

This will set the string ID to "PLANT". I can't find the right path to get to the ID attribute.

<CATALOG>
  <PLANT ID = "821">
     <COMMON>Bloodroot</COMMON>
     <BOTANICAL>Sanguinaria canadensis</BOTANICAL>
     <ZONE>4</ZONE>
     <LIGHT>Mostly Shady</LIGHT>
     <PRICE>$2.44</PRICE>
     <AVAILABILITY>031599</AVAILABILITY>
  </PLANT>
</CATALOG>

I want ID to = 821

Upvotes: 1

Views: 114

Answers (1)

Neel
Neel

Reputation: 11741

Can you give try to the code below:

ID = xmlNodeComplex.ParentNode.Attribute["ID"].Value; 

Upvotes: 1

Related Questions