我有一个长这样的XML文档:
<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>
尽管指定了名称空间(我是从外部源获取的,因此无法控制),但它没有任何可用的模式。我使用 XDocument
进行解析,但在不属于 env
命名空间的项目中仍然得到 null
。我像这样设置 XDocument
:
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");
然后我尝试获取值:
?Source.XPathSelectElement("//kmsg", oManager)
null
?Source.XPathSelectElement("//header", oManager)
null
?Source.XPathSelectElement("//env:source", oManager)
Gets the node correctly
我猜这与我错误设置命名空间管理器有关,但我无法弄清如何修复它。 感谢任何帮助。
谢谢