alexsd
alexsd

Reputation: 113

parsing SOAP response in C#...not understanding the XML namespaces

Please excuse me, I'm new to SOAP and C#. I can't seem to figure out how correctly set the namespaces to find a node in a SOAP response.

Here's the response if the web service query returns empty:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:VXWSResponse xmlns:ns="vx.sx">
            <ns:List ns:id="result" />
        </ns:VXWSResponse>
    </soapenv:Body>
</soapenv:Envelope>

Here's the response if it returns data:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:VXWSResponse xmlns:ns="vx.sx">
            <ns:List ns:id="result">
                <ns:Badge>USER DATA</ns:Badge>
            </ns:List>
        </ns:VXWSResponse>
    </soapenv:Body>
</soapenv:Envelope>

I just need to know if the tag is present.

Here's what I have so far.

XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("ns", "vx.sx");
manager.AddNamespace("id", "result");
xmlNode badge = xml.SelectSingleNode("//id:Badge", manager);
XmlNode result = xml.SelectSingleNode("//ns:result", manager);

Both nodes return null. I've looked at good number of other articles on this site, but I'm not seeing how to correctly address the namespaces in the response XML.

Any help is appreciated!

Upvotes: 2

Views: 2458

Answers (1)

Dave
Dave

Reputation: 518

the id is an attribute of the List Node, not a namespace.

I Edited my answer this to check for the Badge element as that is all you seem to want to look for.

        XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
        manager.AddNamespace("ns", "vx.sx");

        XmlNode badge = xmlDoc.SelectSingleNode("//ns:Badge", manager);

        if (badge == null)
        {
          // no badge element
        }
        else
        {
            // badge element present
        }

Upvotes: 2

Related Questions