Using LINQ to XML to parse a SOAP message

I am getting speed in Linq for XML in C # and trying to parse the following message and doesn't seem to have made much progress. Here is the soap message, I'm not sure, maybe I need to use a namespace. Here is the SOAP message I'm trying to format. Any help would be greatly appreciated. I am trying to extract the values. Thanks.

<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> <Lookup xmlns="http://ASR-RT/"> <objIn> <transactionHeaderData> <intWebsiteId>1000</intWebsiteId> <strVendorData>test</strVendorData> <strVendorId>I07</strVendorId> </transactionHeaderData> <intCCN>17090769</intCCN> <strSurveyResponseFlag>Y</strSurveyResponseFlag> </objIn> </CCNLookup> </soap:Body> </soap:Envelope> 
+4
source share
3 answers

If it concerns interaction with the SOAP service, please use Add Service Link or wsdl.exe .

If it is only an XML parsing, assuming you got a SOAP response in an XDocument called soapDocument:

 XNamespace ns = "http://ASR-RT/"; var objIns = from objIn in soapDocument.Descendants(ns + "objIn") let header = objIn.Element(ns + "transactionHeaderData") select new { WebsiteId = (int) header.Element(ns + "intWebsiteId"), VendorData = header.Element(ns + "strVendorData").Value, VendorId = header.Element(ns + "strVendorId").Value, CCN = (int) objIn.Element(ns + "intCCN"), SurveyResponse = objIn.Element(ns + "strSurveyResponseFlag").Value, }; 

This will give you IEnumerable anonymous types that you are dealing with fully strongly typed objects in this method.

+9
source

Use Linq XDocument to load XML text by calling XDocument.Load() or similar. You can then go through the element tree from the root of xdoc using functions such as

 foreach (var x in xdoc.Elements("Lookup")) {...} 
0
source

You can get your XML in XElement, and then simply:

 rsp.Descendants("Lookup").ToList(); 

Or

rsp.Descendants ("objIn") ToList () ;.

I think this is the best way to do this. I believe XElement is the best choice.

0
source

All Articles