问题描述
我有一个看起来像这样的XML文档:
< kmsg的xmlns =HTTP://为url1的xmlns :ENV =为url1的xmlns:XSI =http://www.w3.org/2001/XMLSchemainstanceXSI:=的schemaLocationHTTP://位置不exist.xsd>
<报头GT;
< ENV:信封>
< ENV:源科=907机=0密码=J123/>
< / ENV:信封>
< /头>
<身体GT;
< OrderResponse的xmlns =金塔:schemasbasdaorg:2000:orderResponse:XDR:3.01>
< SomeMoreNodes />
< / OrderResponse>
< /身体GT;
它不具有任何模式可尽管有指定的命名空间(我是从外部源获得这个所以没有控制)。我有一个XDocument解析它,但不断收到为空值未在ENV命名空间的项目。我设立的XDocument是这样的:
的XDocument源= XDocument.Load(Testfile.xml);
的XmlNamespaceManager oManager =新的XmlNamespaceManager(新NameTable());
oManager.AddNamespace(的String.Emptyhttp://xml.kerridge.net/k8msg);
oManager.AddNamespace(ENV,http://xml.kerridge.net/k8msgEnvelope);
然后我试图得到的值:
?Source.XPathSelectElement(// kmsg,oManager)
空
?Source.XPathSelectElement( //头,oManager)
空
Source.XPathSelectElement(// ENV:源oManager)
正确获取节点
我假定这是一件与我的设置了命名空间管理错了,但我无法弄清楚如何解决它。任何帮助将是巨大的。
感谢
在除通过@ MADS-汉森正确的话,你还没有定义的命名空间中的一个(非空)前缀的典型问题。
记住:XPath的考虑任何前缀的名称是在无空间
因此,这是错误的
Source.XPathSelectElement(// kmsg,oManager)
这XPath表达式要选择在没有命名空间所有的 kmsg
元素,并正确选择什么,因为任何所提供的XML文档中kmsg
元素在的http://为url1
命名空间,而不是在没有命名空间。
要做到正确
oManager。 AddNamespace(XXX,HTTP://为url1);
Source.XPathSelectElement(// XXX:kmsg,oManager)
I have an XML document that looks like this:
<kmsg xmlns="http://url1" xmlns:env="url1" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:schemaLocation="http://location that does not exist.xsd">
<header>
<env:envelope>
<env:source branch="907" machine="0" password="J123"/>
</env:envelope>
</header>
<body>
<OrderResponse xmlns="urn:schemasbasdaorg:2000:orderResponse:xdr:3.01">
<SomeMoreNodes/>
</OrderResponse>
</body>
It does not have any schemas available despite having namespaces specified (I'm getting this from an external source so have no control). I'm parsing it with an XDocument, but keep getting nulls for the items not in the env namespace. I'm setting up the XDocument like this:
XDocument Source = XDocument.Load("Testfile.xml");
XmlNamespaceManager oManager = new XmlNamespaceManager(new NameTable());
oManager.AddNamespace(String.Empty, "http://xml.kerridge.net/k8msg");
oManager.AddNamespace("env", "http://xml.kerridge.net/k8msgEnvelope");
Then I try to get values:
?Source.XPathSelectElement("//kmsg", oManager)
null
?Source.XPathSelectElement("//header", oManager)
null
?Source.XPathSelectElement("//env:source", oManager)
Gets the node correctly
I'm assuming this is something to do with me setting up the namespace manager wrong but I can't figure out how to fix it. Any help would be great.
Thanks
In addition to the correct remark by @Mads-Hansen, you have the typical problem of not defining a (nonempty) prefix for one of the namespaces.
Remember: XPath considers any unprefixed name to be in "no namespace".
Therefore this is wrong:
Source.XPathSelectElement("//kmsg", oManager)
This XPath expression wants to select all kmsg
elements that are in "no namespace" and it correctly selects nothing, because any kmsg
elements in the provided XML document are in the "http://url1"
namespace, and not in "no namespace".
To do it correctly:
oManager.AddNamespace("xxx", "http://url1");
Source.XPathSelectElement("//xxx:kmsg", oManager)
这篇关于古怪与的XDocument,XPath和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!