Search code examples
c#web-servicessoapexception

Read XMLNode errors from SoapException


I have this kind of SoapException

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Server</faultcode>
            <faultstring>Fault occurred while processing.</faultstring>
            <detail>
                <ns1:WaybillRegistrationFault xmlns:ns1="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
                    <errors xmlns:ns2="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
                        <code>80000</code>
                        <description>El número de CTG 59455243 ya existe</description>
                    </errors>
                    <errors xmlns:ns2="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
                        <code>1000</code>
                        <description>Unexpected Error</description>
                    </errors>
                </ns1:WaybillRegistrationFault>
            </detail>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

How do I read each error? I tried with Detail.InnerText but all the text shows without formatting. Is there a way of using foreach on the tag?


Solution

  • You can parse that xml easily with LinqToXml

    var errors = XDocument.Parse(yourxmlstring)
                    .Descendants("errors")
                    .Select(e => new
                    {
                        code = (int)e.Element("code"),
                        desc = (string)e.Element("description")
                    })
                    .ToList();