在XmlDocument的名称搜索节点 [英] Search for nodes by name in XmlDocument
本文介绍了在XmlDocument的名称搜索节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找到名称的节点与下面的代码一个XmlDocument:
I'm trying to find a node by name in an XmlDocument with the following code:
private XmlNode FindNode(XmlNodeList list, string nodeName)
{
if (list.Count > 0)
{
foreach (XmlNode node in list)
{
if (node.Name.Equals(nodeName)) return node;
if (node.HasChildNodes) FindNode(node.ChildNodes, nodeName);
}
}
return null;
}
我打电话的功能:
I call the function with:
FindNode(xmlDocument.ChildNodes, "somestring");
有关某种原因,它始终返回null,我真的不知道为什么。有人可以帮我这个
For some reason it always returns null and I'm not really sure why. Can someone help me out with this?
推荐答案
更改此行:
if (node.HasChildNodes) FindNode(node.ChildNodes, nodeName);
到
to:
if (node.HasChildNodes)
{
XmlNode nodeFound = FindNode(node.ChildNodes, nodeName);
if (nodeFound != null)
return nodeFound;
}
编辑:现在的代码(测试)是比较正确的;)
EDITED: the code is more correct now (tested) ;)
这篇关于在XmlDocument的名称搜索节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文