Question

I am trying to parse a heavily namespaced SOAP message (source can be found also here):

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Header>
  <ns1:TransactionID soapenv:mustUnderstand="1" xsi:type="xsd:string" xmlns:ns1="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2">0a00f556419041c08d8479fbaad02a3c</ns1:TransactionID>
 </soapenv:Header>
 <soapenv:Body>
  <SubmitRsp xmlns="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2">
   <MM7Version>5.3.0</MM7Version>
   <Status>
    <StatusCode xsi:type="ns2:responseStatusType_StatusCode" xmlns:ns2="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2" xmlns="">1000</StatusCode>
    <StatusText xsi:type="ns3:statusTextType" xmlns:ns3="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2" xmlns="">Success</StatusText>
   </Status>
   <MessageID>B08CF7B847DAD89C752334BDEBB69B5B</MessageID>
  </SubmitRsp>
 </soapenv:Body>
</soapenv:Envelope>

Just for the context, this is a response of MM7 Submit message.

How can I get the following values:

TransactionID, StatusCode, StatusText, MessageID

I tried Linq-Xml but no luck when the query includes a value like "soapenv:Body".

Was it helpful?

Solution

If you're trying to build an XName with a namespace you need to build it from an XNamespace plus a string, e.g.

XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
XName body = soapenv + "Body";

Then when you use the XName "body" with Linq-to-XML it will match the <soapenv:Body> element in your document.

You can do similar things to allow building the names of other elements with namespaces.

OTHER TIPS

There's an even simpler way. You can simply specify the namespace inline using {} notation:

var soap = XElement.Load(soapPath);
var transactionID = 
        from e in soap.Descendants("{http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2}TransactionID")
        select e.Value;

I think you will need to use XmlDocument (for reading the XML) and XmlNamespaceManager (for retreiving the namespace data) and using XPath queries from those objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top