开发者

XML XPath SelectSingleNode issues

开发者 https://www.devze.com 2023-03-24 04:09 出处:网络
I am trying to select certain nodes inside of childnodes of the main document. XmlNodeList nodes = xml.SelectNodes(\"//RECORD\");

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 f开发者_运维技巧rom 1 to 32 for example.

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


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


It is because you are using //content2 in the XPATH for nodes[i].SelectSingleNode. // in XPATH means look at all instances at any level. You should do an XPATH of //RECORD/content2 then loop those nodes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号