Parse and query SOAP in C#

冷暖自知 提交于 2019-12-06 10:37:04

问题


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".


回答1:


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.




回答2:


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;



回答3:


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.



来源:https://stackoverflow.com/questions/1265490/parse-and-query-soap-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!