Reputation: 25711
I have code like so:
string xml = "<root><span tag=\"LUMP\" missingValue=\"3,4,5,6,7,8\" format=\"Disc\" varName=\"RACE\" label=\"Race/ethnicity\"><element value=\"1+2\" label=\"Total 1+2\" /><element value=\"1\" label=\"White\" /><element value=\"2\" label=\"Black or African American\" /></span></root>";
doc.LoadXml(xml);
XmlNodeList varsList = doc.SelectNodes("span");
But everytime varsList is empty. Why?
Upvotes: 3
Views: 5173
Reputation: 3475
I'm not sure why doc.SelectNodes() isn't working for you, but using a XmlNode should give you the list that you want.
...
doc.LoadXml(xml);
XmlNode root = doc.DocumentElement;
XmlNodeList varsList = root.SelectNodes("span");
Upvotes: 0
Reputation: 100547
"span" XPath means "immediate child nodes with name span
". Since immediate child is root
you get nothing.
You want either "//span"
(all spans anywhere in the tree starting from root) or "/root/span"
("root" at root, than its "span" children).
Upvotes: 4