Home > front end >  Adding namespace to SOAP API response to access elements in the response using XmlNamespaceManager i
Adding namespace to SOAP API response to access elements in the response using XmlNamespaceManager i

Time:11-13

I am trying to access all elements int this SOAP response, but I am finding it difficult to access the elements inside the response. I am trying to use XmlNamespaceManager, but do not know how to go about it. Please can anyone give me an insight?

Below is the response;


<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header/>
    <soap:Body>
        <Result>
            <statuscode>200</statuscode>
            <Customer>10001252</Customer>
            <CustomerName>MBOKO FARMS</CustomerName>
            <CustomerFullName>MBOKO FARMS/0 ANAMBRA</CustomerFullName>
            <FiscalAddress/>
            <VATRegistration/>
        </Result>
    </soap:Body>
</soap:Envelope>

and also;

I tried using XmlNamespaceManager, but forming the addnamsespace is giving me tough time. I want to access the different elements from this response.

CodePudding user response:

If you just need to read that message, then easy solution is to copy the whole message. Create a new cs file in Visual Studio, and clear it. Then Edit->Paste Special->Paste Xml As Classes.

Then


using System.Xml.Serialization;

internal class Program
{
    static void Main(string[] args)
    {
        Envelope envelope = Read(@"C:\temp\soap_xml.xml");

        var name = envelope.Body.Result.CustomerFullName;
    }


    private static Envelope Read(string file)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
        using (StreamReader reader = new StreamReader(file))
        {
            return (Envelope)serializer.Deserialize(reader)!;
        }
    }
}
  • Related