0

I am trying to integrate a web service with my project. The web service returns an XML response:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
                   xmlns:ns1="http://www.tpg.ua/" 
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
                   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:getCitiesResponse>
            <return xsi:type="xsd:string">
                [
                    {
                        "cityTitle": "Тирана",
                        "countryId": "7",
                        "cityId": "2493",
                        "IATA": "TIA"
                    },
                    {
                        "cityTitle": "Ла-Романа",
                        "countryId": "48",
                        "cityId": "1280",
                        "IATA": "LRM"
                    },
                    {
                        "cityTitle": "Херсон",
                        "countryId": "145",
                        "cityId": "2719",
                        "IATA": "KHE"
                    },
                    {
                        "cityTitle": "Шарм",
                        "countryId": "49",
                        "cityId": "2851",
                        "IATA": "SSH"
                    }
                ]
            </return>
        </ns1:getCitiesResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I am trying to parse that response. I want to get the cityId and the countryId

XmlDocument listXML = new XmlDocument();
listXML.LoadXml(response);

string baseNode = @"return/";

XmlNode item1 = listXML.SelectSingleNode(baseNode + "countryId"); 
XmlNode item2 = listXML.SelectSingleNode(baseNode + "cityId");

string s = item1.InnerText.ToString();
string s1 = item2.InnerText.ToString();

But it is null; what should I do?

1 Answer 1

2

The return element of your SOAP response contains a JSON string, so you cannot use XmlDocument and an XPath query to extract data from that.

You need to query the JSON data with a JSON deserializer, for example I have used JSON.NET in this example - you can add this to your project by installing the NuGet package Newtonsoft.Json.

So there are two steps here - firstly extract the content of the return element from the XML response. I use XDocument rather than XmlDocument as you have used, as XDocument is a more modern API and plays nicer with modern language features such as Linq.

Second step is to deserialise the JSON content, and extract the content into variables.

var xDoc = XDocument.Parse(response);
var returnJson = xDoc.Descendants("return")
    .Single()
    .Value;

var cities = JArray.Parse(returnJson);
foreach (var city in cities)
{
    var cityId = city["cityId"].Value<string>();
    var countryId = city["countryId"].Value<string>();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.