简体   繁体   English

如何在xml文件中将字符串设置为属性值?

[英]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. 这使我可以在4区中搜索植物。找到一个植物后,我希望能够从上面的属性中获取ID#。 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". 这会将字符串ID设置为“ PLANT”。 I can't find the right path to get to the ID attribute. 我找不到找到ID属性的正确路径。

<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 我希望ID = 821

您可以尝试以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM