XmlDocument.SelectSingleNode()&名称空间 - 不起作用 [英] XmlDocument.SelectSingleNode() & namespaces - doesn't work

查看:77
本文介绍了XmlDocument.SelectSingleNode()&名称空间 - 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨;


如果没有命名空间,这对我来说很好。但是如果xml有
命名空间,那么我得到的节点都没有或者是异常。


这里是示例xml:

< root xmlns =" http://www.test.org"

xmlns:sns =" http://www.test.org/sub"

xmlns:mns =" http://www.test.org/mini">

< data>

< items>

< item id =" 1">

< sns:subItem sid =" 11"> dave< / sns:subItem>

< sns:subItem sid =" 12"> thielen< / sns:subItem>

< / item>

< item id =" 2">

< sns:subItem sid =" 21"> shirley< / sns:subItem>

< / item>

< / items>

< mns:more> dave thielen< / mns:more>

< / data>

< / root>


1.如果我选择SelectSingleNode(" / root / data / items / item / @ id") - 我没有节点

返回。如果我创建一个XmlNamespaceManager就是这种情况。


2.如果我选择SelectSingleNode(" / root / data / items / item / sns:subItem / @sid")

我有一个命名空间限定节点我得到:


2a。如果没有XmlNamespaceManager,我会得到一个例外。


2b。如果我添加以下代码:

XmlNamespaceManager context = new XmlNamespaceManager(doc.NameTable);

context.AddNamespace(" sns"," http:// www .test.org / sub");

node = doc.SelectSingleNode((" / root / data / items / item / sns:subItem / @ sid",

context);

然后我没有返回任何节点(但至少没有抛出异常)。


我测试了XmlSpy和xpath中的所有内容按预期工作。


那么发生了什么?


首先,即使我添加命名空间,我仍然可以''得到任何节点

返回。具有名称空间的那些和没有评估到没有节点的那些。


其次,如果我不这样做,它为什么会抛出异常不创建命名空间

manager?命名空间存在于xml文件中,根据定义,应该使用那些

映射。另外,引用来自
<一个rel =nofollowhref =http://msdn.microsoft.com/library/default.asp? url = / library / en-us / cpguide / html / cpconxpathquerieswithnamespacedmappedprefixes.asptarget =_ blank> http://msdn.microsoft.com/library/de...edprefixes.asp

"如果没有将msbooks命名空间添加到XmlNamespaceManager,并且如果它不是
不在文档的名称表中,则抛出异常。所以

应该没有为

文件中定义的命名空间抛出异常。


求助 - 请不要感觉对我来说。


-

谢谢 - 戴夫

Hi;

If there are no namespaces this works fine for me. But if the xml has
namespaces, then I get either no node back or an exception.

Here is the sample xml:
<root xmlns="http://www.test.org"
xmlns:sns="http://www.test.org/sub"
xmlns:mns="http://www.test.org/mini">
<data>
<items>
<item id="1">
<sns:subItem sid="11">dave</sns:subItem>
<sns:subItem sid="12">thielen</sns:subItem>
</item>
<item id="2">
<sns:subItem sid="21">shirley</sns:subItem>
</item>
</items>
<mns:more>dave thielen</mns:more>
</data>
</root>

1. If I do SelectSingleNode("/root/data/items/item/@id") - I get no nodes
returned. This is the case if I create an XmlNamespaceManager or not.

2. If I do SelectSingleNode("/root/data/items/item/sns:subItem/@sid") where
I have a namespace qualified node I get:

2a. If there is no XmlNamespaceManager I get an exception.

2b. If I add the following code:
XmlNamespaceManager context = new XmlNamespaceManager(doc.NameTable);
context.AddNamespace("sns", "http://www.test.org/sub");
node = doc.SelectSingleNode(("/root/data/items/item/sns:subItem/@sid",
context);
Then I get no nodes returned (but at least no exception is thrown).

I tested all of the in XmlSpy and the xpath works there as expected.

So what is going on?

First off, even if I add the namespaces, I still can''t get any node
returned. Both those with namespaces and those without evaluate to no nodes.

Second, why does it throw an exception if I don''t create a namespace
manager? The namespaces exist in the xml file and by definition, those
mappings should be used. Also, to quote from
http://msdn.microsoft.com/library/de...edprefixes.asp
"If the msbooks namespace was not added to the XmlNamespaceManager, and if it
is not in the name table of the document, then an exception is thrown." So
there should be no exception thrown for namespaces that are defined in the
document.

Help please - this makes no sense to me.

--
thanks - dave

推荐答案

嗨Dave,


首先,我想确认一下我对你的问题的理解。从

您的描述,我知道您正在尝试使用SelectSingleNode

来获取属性。如果有任何误解,请随时告诉我



由于节点在默认命名空间下,SelectSingleNode需要

我们为Xpath表达式中的每个节点指定名称空间,包括

默认名称空间。您可以使用

以下代码获取/ root / data / items / item / @ id。


XmlDocument doc = new XmlDocument();

doc.LoadXml(s);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

nsmgr.AddNamespace(" d", " http://www.test.org");

nsmgr.AddNamespace(" sns"," http://www.test.org/sub");

nsmgr.AddNamespace(" mns"," http://www.test.org/mini");

XmlNode node = doc.SelectSingleNode(" // d :root / d:data / d:items / d:item / @ id",

nsmgr);


另外,要获取/ root / data / items / item / sns:subItem / @ sid,请使用

以下代码。


XmlNode node =

doc.SelectSingleNode(" // d:root / d:data / d:items / d:item / sns:subItem / @ sid",

nsmgr);

HTH。


Kevin Yu

=======

"此帖子已提供按现状没有保证,也没有赋予

权利。

Hi Dave,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you are trying to use SelectSingleNode
to get the attribute. If there is any misunderstanding, please feel free to
let me know.

Since the nodes are under the default namespace, SelectSingleNode require
us to specify the namespace for each node in the Xpath expression including
the default namespace. You can get /root/data/items/item/@id with the
following piece of code.

XmlDocument doc =new XmlDocument();
doc.LoadXml(s);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("d", "http://www.test.org");
nsmgr.AddNamespace("sns", "http://www.test.org/sub");
nsmgr.AddNamespace("mns", "http://www.test.org/mini");
XmlNode node = doc.SelectSingleNode("//d:root/d:data/d:items/d:item/@id",
nsmgr);

Also, to get to /root/data/items/item/sns:subItem/@sid, Please use the
following code.

XmlNode node =
doc.SelectSingleNode("//d:root/d:data/d:items/d:item/sns:subItem/@sid",
nsmgr);

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


你好;


我的问题是我没有硬编码xpath - 我必须接受

用户输入的内容。而且我必须接受任何合法的xpath并且输入是合法的

默认命名空间中的节点上没有命名空间。


如何处理任何合法的xpath字符串? (换句话说,

的工作方式与XmlSpy,dom4j,XSLT以及其他接受

xpath的程序相同。)


-

谢谢 - 戴夫

" Kevin Yu [MSFT]"写道:
Hello;

My problem is that I am not hardcoding the xpath - I have to accept what the
user types in. And I have to accept any legal xpath and it is legal to enter
it with no namespace on the nodes that are in the default namespace.

How can I get this to handle any legal xpath string? (In other words, to
work the same way XmlSpy, dom4j, XSLT, and every other program that accepts
xpath works.)

--
thanks - dave
"Kevin Yu [MSFT]" wrote:
嗨Dave,

首先,我想确认一下我对你的问题的理解。从
您的描述中,我了解到您正在尝试使用SelectSingleNode
来获取属性。如果有任何误解,请随时告诉我。

由于节点在默认命名空间下,SelectSingleNode要求我们为每个节点指定命名空间Xpath表达式包括默认命名空间。您可以使用
以下代码获取/ root / data / items / item / @ id。

XmlDocument doc = new XmlDocument();
doc.LoadXml(s );; XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace(" d"," http://www.test.org");
nsmgr。 AddNamespace(" sns"," http://www.test.org/sub");
nsmgr.AddNamespace(" mns"," http://www.test.org/mini" );;
XmlNode node = doc.SelectSingleNode(" // d:root / d:data / d:items / d:item / @ id",
nsmgr);

XmlNode node =
doc。 SelectSingleNode(" // d:root / d:data / d:items / d:item / sns:subItem / @ sid",
nsmgr);

HTH。

Kevin Yu
=======
这个帖子是按原样提供的没有保证,也没有授予
权利。
Hi Dave,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you are trying to use SelectSingleNode
to get the attribute. If there is any misunderstanding, please feel free to
let me know.

Since the nodes are under the default namespace, SelectSingleNode require
us to specify the namespace for each node in the Xpath expression including
the default namespace. You can get /root/data/items/item/@id with the
following piece of code.

XmlDocument doc =new XmlDocument();
doc.LoadXml(s);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("d", "http://www.test.org");
nsmgr.AddNamespace("sns", "http://www.test.org/sub");
nsmgr.AddNamespace("mns", "http://www.test.org/mini");
XmlNode node = doc.SelectSingleNode("//d:root/d:data/d:items/d:item/@id",
nsmgr);

Also, to get to /root/data/items/item/sns:subItem/@sid, Please use the
following code.

XmlNode node =
doc.SelectSingleNode("//d:root/d:data/d:items/d:item/sns:subItem/@sid",
nsmgr);

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."



试用SelectSingleNode(" // root / data / items / item [@id =''1'']")

SelectSingleNode(" // root / data / items / item [@id =''2'']")


Harry


" David Thielen"写道:
try out SelectSingleNode("//root/data/items/item[@id=''1'']")
SelectSingleNode("//root/data/items/item[@id=''2'']")

Harry

"David Thielen" wrote:
嗨;

如果没有命名空间,这对我来说很好。但是,如果xml具有名称空间,那么我得到任何节点或异常。

以下是示例xml:
< root xmlns =" http:/ /www.test.org"
xmlns:sns =" http://www.test.org/sub"
xmlns:mns =" http://www.test.org/mini" ;>
< data>
< items>
< item id =" 1">
< sns:subItem sid =" 11"> ; dave< / sns:subItem>
< sns:subItem sid =" 12"> thielen< / sns:subItem>
< / item>
< item id = " 2">
< sns:subItem sid =" 21"> shirley< / sns:subItem>
< / item>
< / items>
< mns:more> dave thielen< / mns:more>
< / data>
< / root>

1.如果我选择SelectSingleNode(& ; / R oot / data / items / item / @ id") - 我没有返回节点
。如果我创建一个XmlNamespaceManager就是这种情况。

2.如果我选择SelectSingleNode(" / root / data / items / item / sns:subItem / @ sid")其中
我得到了一个名称空间限定的节点:

2a。如果没有XmlNamespaceManager,我会得到一个例外。

2b。如果我添加以下代码:
XmlNamespaceManager context = new XmlNamespaceManager(doc.NameTable);
context.AddNamespace(" sns"," http://www.test.org/sub") ;
node = doc.SelectSingleNode((" / root / data / items / item / sns:subItem / @ sid",
context);
然后我没有返回任何节点(但是至少没有异常被抛出。

我测试了XmlSpy中的所有内容,xpath按预期工作。

那是怎么回事?
<首先,即使我添加了名称空间,我仍然无法返回任何节点。无论是名称空间还是没有评估的节点都没有。

其次,如果我不创建命名空间管理器,为什么会抛出异常?命名空间存在于xml文件中,根据定义,应该使用那些
映射。另外,引用来自
http://msdn.microsoft.com/library/de...edprefixes.asp
如果msbooks命名空间未添加到XmlNamespaceManager,如果
不在文档的名称表中,则抛出异常。所以
对于
文档中定义的名称空间应该没有异常。

请帮助 - 这对我没用。

-
谢谢 - 戴夫
Hi;

If there are no namespaces this works fine for me. But if the xml has
namespaces, then I get either no node back or an exception.

Here is the sample xml:
<root xmlns="http://www.test.org"
xmlns:sns="http://www.test.org/sub"
xmlns:mns="http://www.test.org/mini">
<data>
<items>
<item id="1">
<sns:subItem sid="11">dave</sns:subItem>
<sns:subItem sid="12">thielen</sns:subItem>
</item>
<item id="2">
<sns:subItem sid="21">shirley</sns:subItem>
</item>
</items>
<mns:more>dave thielen</mns:more>
</data>
</root>

1. If I do SelectSingleNode("/root/data/items/item/@id") - I get no nodes
returned. This is the case if I create an XmlNamespaceManager or not.

2. If I do SelectSingleNode("/root/data/items/item/sns:subItem/@sid") where
I have a namespace qualified node I get:

2a. If there is no XmlNamespaceManager I get an exception.

2b. If I add the following code:
XmlNamespaceManager context = new XmlNamespaceManager(doc.NameTable);
context.AddNamespace("sns", "http://www.test.org/sub");
node = doc.SelectSingleNode(("/root/data/items/item/sns:subItem/@sid",
context);
Then I get no nodes returned (but at least no exception is thrown).

I tested all of the in XmlSpy and the xpath works there as expected.

So what is going on?

First off, even if I add the namespaces, I still can''t get any node
returned. Both those with namespaces and those without evaluate to no nodes.

Second, why does it throw an exception if I don''t create a namespace
manager? The namespaces exist in the xml file and by definition, those
mappings should be used. Also, to quote from
http://msdn.microsoft.com/library/de...edprefixes.asp
"If the msbooks namespace was not added to the XmlNamespaceManager, and if it
is not in the name table of the document, then an exception is thrown." So
there should be no exception thrown for namespaces that are defined in the
document.

Help please - this makes no sense to me.

--
thanks - dave



这篇关于XmlDocument.SelectSingleNode()&amp;名称空间 - 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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