我有一個看起來像這樣的XML文檔:怪誕用的XDocument,XPath和命名空間
<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命名空間中的項保留空值。我設立的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)
空
?Source.XPathSelectElement( 「//頭」,oManager)
空
?所以, urce.XPathSelectElement(「// ENV:來源」,oManager)
獲取節點正確
我假定這是一件與我建立命名空間管理錯誤的,但我想不出如何解決它。任何幫助都會很棒。
感謝