XmlNode.SelectSingleNode 返回当前之外的元素? [英] XmlNode.SelectSingleNode returns element outside current?

查看:15
本文介绍了XmlNode.SelectSingleNode 返回当前之外的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是这样的.假设我有这样的 xml

my problem is like this. Let's say i have xml like this

<root>
  <child Name = "child1">
    <element1>Value1</element1>
    <element2>Value2</element2>
  </child>
  <child Name = "child2">
    <element1>Value1</element1>
    <element2>Value2</element2>
    <element3>Value3</element3>
  </child>
</root>

我有一个方法可以作为参数 XmlNode节点".让我们说节点"的值为child1"然后我尝试这样:

I have a method that gets as parameter XmlNode "node". Lets say "node" has value "child1" Then i try like this:

node.SelectSingleNode( "//element3" );

问题是这段代码从child2"返回 element3.我想要的是,如果节点"的子element3"没有返回 null,那么我手动添加它.
最好的问候,
爱尔兰

The problem is this code returns element3 from "child2". What i want is if there is no child "element3" of "node" to return null so i add it by hand.
Best Regards,
Iordand

推荐答案

您拥有的 XPath 表达式不是您想要的.

The XPath expression you have isn't what you want.

用这个替换它:

node.SelectSingleNode( "element3" ); 

你会得到你想要的结果.

And you'll get the result you're looking for.

这篇关于XmlNode.SelectSingleNode 返回当前之外的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆