1
votes

I am trying to make a generic XmlParsing method. Take the Xml as such:

<body>
<section>
    <subsection1>
        ...
    </subsection1>
    <subsection2>
        ...
    </subsection2>
</section>
<section>
    <subsection1>
        ...
    </subsection1>
    <subsection2>
        ...
    </subsection2>
</section>
</body>

I am trying to grab all "section" nodes without knowing how deep they are or their parent nodes names.

So far I have (my XML is in string format)

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(XMLtoRead);

        XmlNodeList nodes = xml.DocumentElement.SelectNodes("//section");

However the node count is always 0. I was under the impression the "//" prefix recursivly searches through the document for the nodes named.

My real XML is a SOAP reply:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">


<soap:Body>
    <Response xmlns="http://tempuri.org/">
1
Is there a Namespace URL in your xml document? - LocEngineer
Yea there is a number of urls within the top level elements in my xml document. xmlns="some url". - colobusgem
My XML is SOAP reply, see question edit - colobusgem

1 Answers

3
votes

In that case it is not generic but specific to your kind of SOAP replies. ;-) Try this:

var ns = new XmlNamespaceManager(xml.NameTable);
ns.AddNamespace("ns", "http://tempuri.org/");
XmlNodeList nodes = xml.DocumentElement.SelectNodes("//ns:section", ns);