Using LINQ to XML to Parse a SOAP message

前端 未结 3 1915
终归单人心
终归单人心 2020-12-09 23:39

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 sur

相关标签:
3条回答
  • 2020-12-09 23:50

    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.

    0 讨论(0)
  • 2020-12-10 00:03

    You can get your XML into an XElement an then just do:

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

    Or

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

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

    0 讨论(0)
  • 2020-12-10 00:09

    Use Linq's XDocument to load the XML text by calling XDocument.Load() or similar. Then you can walk through the tree of elements off the xdoc's Root, using functions like

    foreach (var x in xdoc.Elements("Lookup"))
    {...}
    
    0 讨论(0)
提交回复
热议问题