Note that there are some explanatory texts on larger screens.

plurals
  1. POWeirdness with XDocument, XPath and namespaces
    primarykey
    data
    text
    <p>I have an XML document that looks like this:</p> <pre><code>&lt;kmsg xmlns="http://url1" xmlns:env="url1" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:schemaLocation="http://location that does not exist.xsd"&gt; &lt;header&gt; &lt;env:envelope&gt; &lt;env:source branch="907" machine="0" password="J123"/&gt; &lt;/env:envelope&gt; &lt;/header&gt; &lt;body&gt; &lt;OrderResponse xmlns="urn:schemasbasdaorg:2000:orderResponse:xdr:3.01"&gt; &lt;SomeMoreNodes/&gt; &lt;/OrderResponse&gt; &lt;/body&gt; </code></pre> <p></p> <p>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:</p> <pre><code>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"); </code></pre> <p>Then I try to get values:</p> <p>?Source.XPathSelectElement("//kmsg", oManager)</p> <p>null</p> <p>?Source.XPathSelectElement("//header", oManager)</p> <p>null</p> <p>?Source.XPathSelectElement("//env:source", oManager)</p> <p>Gets the node correctly</p> <p>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.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload