Question

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?

Was it helpful?

Solution

"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).

OTHER TIPS

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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top