Search code examples
c#xmlasp.net-corewcfsoap

Parse data from Soap Envelop request


I have soap envelope request and I want to extract some data from element.

Below is a soap envelope request in a string format and I want to extract data that is resides under "<![CDATA[[datafile]". Is there any way I can do that?

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://ACORD.org/Standards/Life/445" xmlns:aaa="http://Life/PolicyDetails/3" xmlns:date="http://exslt.org/dates-and-times">
    <soapenv:Header/>
    <soapenv:Body>
        <aaa:getPolicyDetailResponse>
            <policyInfoResp>
                <ns:TXLife>
                    
                    <ns:TXLifeResponse PrimaryObjectID="Holding_1">
                        <ns:TransRefGUID>rsf4--41c3-8981-5a2c4518a7</ns:TransRefGUID>
                        <ns:TransType tc="201">Inforce Policy Inquiry</ns:TransType>
                        <ns:TransExeDate>2020-10-27-05:00</ns:TransExeDate>
                        <ns:TransExeTime>00:49:16-05:00</ns:TransExeTime>
                        <ns:TransMode tc="2">Original</ns:TransMode>
                        <ns:NoResponseOK tc="0">False</ns:NoResponseOK>
                        <ns:TestIndicator tc="0">False</ns:TestIndicator>           
                        <ns:OLifEExtension VendorCode="AG" ExtensionCode="INI">
                            <![CDATA[
                            <![CDATA[[datafile]
Agent.CityState=ATLANTA, GA
Agent.Comp=AGL
Agent.Name=YRVFL KYCHBPO K               
Agent.Number=000B078A
Agent.Status=Inactive
Agent.StrAddr=4286 DIGKGWZ DNVK KO DYG 202            
]]]]>
                            <![CDATA[>]]>
                        </ns:OLifEExtension>
                    </ns:TXLifeResponse>
                </ns:TXLife>
            </policyInfoResp>
        </aaa:getPolicyDetailResponse>
    </soapenv:Body>
</soapenv:Envelope>

Below is the code I wrote to parse data but I am getting null reference exception.

string contents = System.IO.File.ReadAllText(@"C:\Users\reg\CaReg-output1.txt");
                var document = XDocument.Parse(contents);
                string url = "http://ACORD.org/Standards/Life/2";
                var ns = XNamespace.Get(url);
                var xElements = document.Element(ns+"OLifEExtension").Value.ToString();

Solution

  • Try following :

               string contents = System.IO.File.ReadAllText(@"C:\Users\reg\CaReg-output1.txt");
                XDocument document = XDocument.Parse(contents);
                XElement root = document.Root;
                XNamespace ns = root.GetNamespaceOfPrefix("ns");
    
                List<string> OLifEExtension = document.Descendants(ns + "OLifEExtension").Select(x => (string)x).ToList();