Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

c# - Using LINQ to XML to Parse a SOAP message

I am getting up to speed in Linq to XML in C# and attempting to parse the following message and don't seem to be making much progress. Here is the soap message I am not sure if I perhaps I need to use a namespace. Here is the SOAP message I am trying to format. Any help would be greatly appreciated. I am attempting 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If this is about interacting with a SOAP service please use Add Service Reference or wsdl.exe.

If this is just about parsing the XML, assuming you've got the SOAP response into an XDocument named 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,
    };

That will give you an IEnumerable of anonymous types you deal with as fully strongly typed objects within that method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...