Given a XmlNode containing the following XML fragment, how do I fill XmlNodeList with book nodes?
XMLNode nodeLibrary contains:
<library>
<book>
<title>Three Little Pigs</title>
</book>
<book>
<title>Batman</title>
</book>
<address>123 Main St.</address>
<phone>111-111-1111</phone>
</library>
This should be really easy but I can’t figure it out:
A) Cannot implicitly convert type ‘System.Xml.XmlElement’ to ‘System.Xml.XmlNodeList’:
XmlNodeList books = nodeLibrary[“book”];
I guess the method property shortcut above assumes there’s a SINGLE child named book, not multiple!
B) XmlNode doesn’t have a GetChildren() method:
XmlNodeList books = nodeLibrary.GetChildren(“book”);
C) XmlNode’s ChildNodes property gets ALL children, not just book nodes.
D) I tried using SelectNodes() method but the root is the larger document, not the library fragment in the current XmlNode that was selected from a larger document earlier using SelectNodes.
Any ideas?
Pete
You can use SelectNodes, and in the XPath query pass a ‘.’ to start searching from that node: