Namespace Traversal

Posted by RikSaunderson on Stack Overflow See other posts from Stack Overflow or by RikSaunderson
Published on 2012-10-16T15:35:02Z Indexed on 2012/10/16 17:01 UTC
Read the original article Hit count: 202

Filed under:
|
|

I am trying to parse the following sample piece of XML:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soapenv:Body>
        <d2LogicalModel modelBaseVersion="1.0" xmlns="http://datex2.eu/schema/1_0/1_0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://datex2.eu/schema/1_0/1_0 http://datex2.eu/schema/1_0/1_0/DATEXIISchema_1_0_1_0.xsd">
            <payloadPublication xsi:type="PredefinedLocationsPublication" lang="en">
                <predefinedLocationSet id="GUID-NTCC-VariableMessageSignLocations">
                    <predefinedLocation id="VMS30082775">
                        <predefinedLocationName>    
                            <value lang="en">VMS M60/9084B</value>
                        </predefinedLocationName>
                    </predefinedLocation>
                </predefinedLocationSet>
            </payloadPublication>
        </d2LogicalModel>
    </soapenv:Body>
</soapenv:Envelope>

I specifically need to get at the contents of the top-level predefinedLocation tag. By my calculations, the correct XPath should be

/soapenv:Envelope/soapenv:Body/d2LogicalModel/payloadPublication/predefinedLocationSet/predefinedLocation

I am using the following C# code to parse the XML: string filename = "content-sample.xml";

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filename);

        XmlNamespaceManager nsmanager = new XmlNamespaceManager(xmlDoc.NameTable);
        nsmanager.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/Envelope");

        string xpath ="/soapenv:Envelope/soapenv:Body/d2LogicalModel/payloadPublication/predefinedLocationSet/predefinedLocation";
        XmlNodeList itemNodes = xmlDoc.SelectNodes(xpath, nsmanager);

However, this keeps coming up with no results. Can anyone shed any light on this, because I feel like I'm banging my head on a brick wall.

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml