C# SelectSingleNode - 可以递归使用吗?
例如,如果我有一个 XML 文档,
<root a="value">
<item name="first">
x
<foo name = "firstgrandchild">There is nothing here</foo>
y
<foo name = "secondgrandchild">There is something here</foo>
</item>
<item name="second">
xy
<foo/>
ab
</item>
</root>
我想首先找到节点“item”的第一次出现,然后更新属性,然后我想更新节点“foo”的第一次出现,然后更新属性等,
我的代码如下
myDoc.Load("Items2.xml");
myNode = myDoc.DocumentElement;
mySearchNode = myNode.SelectSingleNode("/root/item");
mySearchNode.Attributes["name"].Value = "Joel";
Console.WriteLine(mySearchNode.OuterXml);
mySearchChildNode = mySearchNode.SelectSingleNode("/item/foo");
Console.WriteLine(mySearchChildNode.OuterXml);
虽然,属性的第一次搜索和更新工作正常,但第二次失败,因为 mySearchNode.SelectSingleNode 返回 null。
问题 - 这段代码是否有根本性的错误? 为什么 SelectSingleNode 在第二个实例中没有按预期工作,就其而言,我在 Element 类型的 XmlNode 上执行它。
请协助。
非常感谢,
As in, if I have an XML Document
<root a="value">
<item name="first">
x
<foo name = "firstgrandchild">There is nothing here</foo>
y
<foo name = "secondgrandchild">There is something here</foo>
</item>
<item name="second">
xy
<foo/>
ab
</item>
</root>
I want to first find the first occurrence of node "item" and then update the attribute, and then I want to update the first occurrence of node "foo" and then update the attribute etc.,
My Code is as below
myDoc.Load("Items2.xml");
myNode = myDoc.DocumentElement;
mySearchNode = myNode.SelectSingleNode("/root/item");
mySearchNode.Attributes["name"].Value = "Joel";
Console.WriteLine(mySearchNode.OuterXml);
mySearchChildNode = mySearchNode.SelectSingleNode("/item/foo");
Console.WriteLine(mySearchChildNode.OuterXml);
While, the first search and update of attribute works fine, the second one fails as mySearchNode.SelectSingleNode returns null.
Question - Is there something fundamentally that is wrong with this code?
Why does not the SelectSingleNode work as expected in the second instance, as far as it is concerned, I am executing it on a XmlNode of type Element.
Kindly assist.
Many Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的第二个 XPath 查询应该不带前导斜杠。 / 表示“文档的根”。 如果省略斜杠,查询将相对于您的 mySearchNode 变量。 您也不应该再次包含“item”,您的查询与您选择的“item”节点相关。 在代码中:
Your second XPath query should be without the leading slash. / means "root of document". If you omit the slash, the query will be relative to your mySearchNode variable. You also should not include "item" again, your query is relative to the "item" node you selected. In code:
mySearchNode 是
item
节点,因此如果foo
是item
的子节点,您的第二个 xpath 应该只是"foo"
mySearchNode is the
item
node, so iffoo
is a child ofitem
your second xpath should simply be"foo"
您可以执行 SelectNodes,然后循环遍历所有项目节点。 对于每个项目,您应该进一步在 foo 节点上执行 SelectNodes。 您应该检查节点计数以及 item 和 foo 节点的属性名称是否存在。
您可以使用此代码:
You can do a SelectNodes and then loop through all the item nodes. For each item you should further do a SelectNodes on foo nodes. You should check for the node count and also whether the attribute name exists or not for both item and foo nodes.
You can use this code:
根据我的理解,主要问题是,在节点上调用的 SelectNode(此处:mySearchNode.SelectSingleNode)无法按预期工作!!!
它不会从给定节点开始搜索,而是始终从根文档开始搜索。 (另请参阅源代码,其中对于节点,相关的 XmlDocument 节点用于启动 XPathNavigator)
我根本没想到会有这种行为!
最好使用应该按预期工作的 XDocument。
From my understanding the main problem is, that SelectNode(s) called on a node (here: mySearchNode.SelectSingleNode) does not work as expected!!!
It does not search beginning with the given node, but always from root document. (see also the source code, where for a node the related XmlDocument node is used to initiate the XPathNavigator)
I did not expect this behaviour at all!
Better use XDocument that should work as expected.