XML Cannot get value of child node: C #

I'm new to XML ^ _ ^

<a>
<book>
  <c>
       <e>Val1</e>
  </c>
  <d>val2</d>
</book>
<book>
  <c>
       <e>Val3</e>
  </c>
  <d>val4</d>
</book>

Question: I need to get value inside each book first I use

XmlNodeList xnList = xDoc.SelectNodes("/a/book");

the problem node "c" has a child element "e", so I can not get its value, as I get directly from node "d"

foreach (XmlNode xn in xnList)
            {
                string Name = xn["e"].InnerText;   // Can't get its value
                string Detail = xn["d"].InnerText;
            }

thank

+3
source share
1 answer

You need to select the trays below the <book>node:

XmlNodeList xnList = xDoc.SelectNodes("/a/book");

foreach (XmlNode xn in xnList)
{
    XmlNode eNode = xn.SelectSingleNode("c/e");

    if(eNode != null)
    {
         string Name = eNode.InnerText;
    }
}
+5
source

Source: https://habr.com/ru/post/1750836/


All Articles