Home > Mobile >  Parsing SOAP response in C#
Parsing SOAP response in C#

Time:02-02

I am new C#. I make a SOAP request and in the SOAP response, I need to access repeating nodes 'ABC'. This is how my SOAP Response looks like:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header>
        <work:WorkContext xmlns:work="http://example.com/soap/workarea/">sdhjasdajsdhj=</work:WorkContext>
    </env:Header>
    <env:Body>
        <ReadABCResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.xyz.com/abc/a6/AB/XYZ/V1">
            <ABC xmlns="http://xmlns.xyz.example/abc/a6/AB/XYZ/V1">
                <asd xmlns="http://xmlns.example.com/abc/a6/AB/XYZ/V1" xsi:nil="true"/>
                <xyz xmlns="http://xmlns.example.com/abc/a6/AB/XYZ/V1" xsi:nil="true"/>
            </ABC>
            <ABC xmlns="http://xmlns.example.com/abc/a6/AB/XYZ/V1">
                <asd xmlns="http://xmlns.example.com/abc/a6/AB/XYZ/V1" xsi:nil="true"/>
                <xyz xmlns="http://xmlns.example.com/abc/a6/AB/XYZ/V1" xsi:nil="true"/>
            </ABC>
        </ReadABCResponse>
    </env:Body>
</env:Envelope>

My code is as below:

XmlDocument responseDoc = new XmlDocument();
responseDoc.LoadXml(responseString); //responseString is set to above SOAP response.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(responseDoc.NameTable);
nsmgr.AddNamespace("env", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("work", "http://example.com/soap/workarea/");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("", "http://xmlns.example.com/abc/a6/AB/XYZ/V1");

XmlNodeList lst = responseDoc.SelectNodes("/env:Envelope/env:Body/ReadABCResponse/ABC", nsmgr);
Console.WriteLine("Count "   lst.Count);

// and then iterate over the repeating ABC nodes to do some work.

However value of Count is always printed as 0. I have tried different combinations of the xpath path in "SelectNodes" method including "//ABC" - which I thought should give me all the repeating 'ABC' nodes but it does not.

What is wrong with my code. please can someone highlight and help me!

I have looked around on this site but cant figure out what is wrong with this code.

CodePudding user response:

The following shows how to use XDocument to read data from the XML.

Test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header>
        <work:WorkContext xmlns:work="http://example.com/soap/workarea/">sdhjasdajsdhj=</work:WorkContext>
    </env:Header>
    <env:Body>
        <ReadABCResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.xyz.com/abc/a6/AB/XYZ/V1">
            <ABC xmlns="http://xmlns.xyz.example/abc/a6/AB/XYZ/V1">
                <asd xmlns="http://xmlns.example.com/abc/a6/AB/XYZ/V1" xsi:nil="true">asd data 1</asd>
                <xyz xmlns="http://xmlns.example.com/abc/a6/AB/XYZ/V1" xsi:nil="true">xyz data 1</xyz>
            </ABC>
            <ABC xmlns="http://xmlns.example.com/abc/a6/AB/XYZ/V1">
                <asd xmlns="http://xmlns.example.com/abc/a6/AB/XYZ/V1" xsi:nil="true">asd data 2</asd>
                <xyz xmlns="http://xmlns.example.com/abc/a6/AB/XYZ/V1" xsi:nil="true">xyz data 2</xyz>
            </ABC>
        </ReadABCResponse>
    </env:Body>
</env:Envelope>

Add the following using statements:

  • using System.Xml;
  • using System.Xml.Linq;
  • using System.Diagnostics;

Create a class (name: ABC.cs)

public class ABC
{
    public string Asd { get; set; }
    public string Xyz { get; set; }
}

Option 1:

private void GetABC()
{
    //ToDo: replace with your XML data
    string xmlText = "your XML data...";

    //parse XML
    XDocument doc = XDocument.Parse(xmlText);

    //create new instance
    List<ABC> abcs = new List<ABC>();

    foreach (XElement elem in doc.Descendants().Where(x => x.Name.LocalName == "ABC"))
    {
        //create new instance
        ABC abc = new ABC();

        foreach (XElement elemChild in elem.Descendants())
        {
            //Debug.WriteLine($"{elemChild.Name}: '{elemChild.Value?.ToString()}'");

            if (elemChild.Name.LocalName == "asd")
                abc.Asd = elemChild.Value?.ToString();
            else if (elemChild.Name.LocalName == "xyz")
                abc.Xyz = elemChild.Value?.ToString();
        }

        //add to List
        abcs.Add(abc);
    }

    foreach (ABC abc in abcs)
    {
        Debug.WriteLine($"ABC: '{abc.Asd}' XYZ: '{abc.Xyz}'");
    }
}

Option 2:

private void GetABC()
{
    //ToDo: replace with your XML data
    string xmlText = "your XML data...";

    //parse XML
    XDocument doc = XDocument.Parse(xmlText);

    //get namespace
    XNamespace nsABC = doc.Descendants().Where(x => x.Name.LocalName == "ABC").FirstOrDefault().GetDefaultNamespace();

    List<ABC> abcs = doc.Descendants().Where(x => x.Name.LocalName == "ABC").Select(x2 => new ABC()
    {
        Asd = (string)x2.Element(nsABC   "asd"),
        Xyz = (string)x2.Element(nsABC   "xyz")
    }).ToList();

    foreach (ABC abc in abcs)
    {
        Debug.WriteLine($"ABC: '{abc.Asd}' XYZ: '{abc.Xyz}'");
    }
}

Resources:

CodePudding user response:

You can also do this: copy the xml and paste it in Visual studio using Paste special -> Paste Xml as classes. Now you will have an Envelope class and you can deserialize the xml like in this example:https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer.deserialize?view=net-7.0. Soap messages can be quite complex and it would be easier to work with objects which you can modify and eventually serialize back if needed.

CodePudding user response:

Thanks for your response guys but I managed to solve it. In the namsspace manager, I made the following change

nsmgr.AddNamespace("x", "http://xmlns.example.com/abc/a6/AB/XYZ/V1");

and when listing the ABC nodes, I made the following change i.e. to prefix ABC with x:

XmlNodeList lst = responseDoc.SelectNodes("//x:ABC", nsmgr);

Rest of the code remains as it is and now I can loop through all the ABC nodes.

  • Related