简体   繁体   中英

Getting the certain Attribute in nested XML nodes using c#

I have the XML from the file animals.XML. how can i get the value dog cat and bird. Im new with xml and c# thanks

<config>
   <settingA>
   <settingA/>

   <settingB>
       <add key="as" val="dog"/>
       <add key="bd" val="cat"/>
       <add key="da" val="bird"/>
   <settingB/>

   <settingC>
   <settingC/>
<config/>

//load the XML in memory from a document or other ways

XDocument doc = XDocument.Load("");
var values = doc.Descendants("settingB");
foreach( var value in values )
  {
     Console.WriteLine( value.Value );
  }
Console.ReadLine();

I am also isn't professional, but I think something like this should works.

XmlDocument xml = new XmlDocument();
xml.Load("");// load from file for example
foreach(XmlNode node in xml.DocumentElement["settingB"].ChildNodes){
    Console.WriteLine(node.Attributes["val"].Value);
}
Console.ReadLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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