XPathを使用してXML文書のノードを選択したいとします。しかし、XML文書にxml-namespacesが含まれている場合は機能しません。 名前空間を考慮してXPathでノードを検索するにはどうすればよいですか?XML文書に名前空間が含まれている場合にXPathでノードを選択する
これは私のXMLドキュメント(簡体字)である:私はRemoteTracerタグののTraceListenerタグのすべてのURLのタグを取得したい
<ComponentSettings xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Company.Product.Components.Model">
<Created xmlns="http://schemas.datacontract.org/2004/07/Company.Configuration">2016-12-14T10:29:28.5614696+01:00</Created>
<LastLoaded i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/Company.Configuration" />
<LastSaved xmlns="http://schemas.datacontract.org/2004/07/Company.Configuration">2016-12-14T16:31:37.876987+01:00</LastSaved>
<RemoteTracer>
<TraceListener>
<Key>f987d7bb-9dea-49b4-a689-88c4452d98e3</Key>
<Url>http://192.168.56.1:9343/</Url>
</TraceListener>
</RemoteTracer>
</ComponentSettings>
。 XML文書が名前空間を使用していない場合、これは私はそれらを得る方法ですが、これだけの作品:
componentConfigXmlDocument = new XmlDocument();
componentConfigXmlDocument.LoadXml(myXmlDocumentCode);
var remoteTracers = componentConfigXmlDocument.SelectNodes("//RemoteTracer/TraceListener/Url");
現在、私の回避策は、ロードする前に、正規表現を使用してXML生の文字列からすべての名前空間を削除することですXML。その後、私のSelectNodes()は正常に動作します。しかし、それは適切な解決策ではありません。
[C#で既定の名前空間でXPathを使用]の可能重複します(http:// stackoverflowの.com/questions/585812/using-xpath-with-default-namespace-in-c-sharp) –
StackOverflowにこの質問に対する1000の答えがあります。 –