Search code examples
c#xmlxpathselectsinglenode

XML XPath SelectSingleNode issues


I am trying to select certain nodes inside of childnodes of the main document.

XmlNodeList nodes = xml.SelectNodes("//RECORD");

for (int i = 0; i < nodes.Count; i++)
{           
     Console.WriteLine("Time: " + nodes[i].SelectSingleNode("//content2").InnerText);
}

What this ends up doing, is that everything single content2 node, has the same value, yet in the XmlDocument, the content2 value for each 'Record' is incremented.

If I look through the NodeList, each content2 node is incremented from 1 to 32 for example.

Why does SelectSingleNode return the same node? How do I make it select it from the child?


Solution

  • Omit the "//" from your XPATH string. This will look inside of the selected XML instead of the entire document.