Kevinc
Kevinc

Reputation: 166

monodevelop parse soap response

been reading this and others forums for hours and days now and can't find a solution for my soap response. Been trying all kinds of answers here, but can't parse my response :(

my response :

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <getLocationsResponse xmlns="http://getlocations.ws.hymedis.net">
            <locs>
                <loc>
                    <name>Zeebrugge Wielingen Zand</name>
                    <abbr>ZWZ</abbr>
                    <RDX>1435.8</RDX>
                    <RDY>378678.6</RDY>
                    <params>
                        <param>GHs</param>
                        <param>SS10</param>
                    </params>
                </loc>
            </locs>
        </getLocationsResponse>
    </soapenv:Body>
</soapenv:Envelope>

My c# code so far (param soapresponse is the whole soapresponse in string format) my response is correct, so the full xml soap response, but can't parse it good

public void readXml(string soapresponse){
        XmlDocument xmlresponse = new XmlDocument();
        xmlresponse.LoadXml(soapresponse);

        XmlNamespaceManager nsmanager = new XmlNamespaceManager(xmlresponse.NameTable);
        nsmanager.AddNamespace ("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");

        XmlNodeList nodes = xmlresponse.SelectNodes("/soapenv:Envelope/soapenv:Body/getLocationsResponse/locs/loc", nsmanager);
        List<Locatie> locatielijst = new List<Locatie>();
        // loop
        foreach(XmlNode node in nodes){
            string loc_naam = node["name"].InnerText;
            string loc_code = node["abbr"].InnerText;
            ...
            Locatie locatie = new Locatie();
            locatie.loc_naam = loc_naam;
            locatie.loc_code = loc_code;
            ...
            locatielijst.Add (locatie);
        }

        Console.WriteLine(locatielijst.Count.ToString());
        foreach(Locatie loc in locatielijst){
            Console.WriteLine (loc.loc_code);
        }

    }

but every time my list.count returns 0 -> so no data in them.. plz help me out!

Upvotes: 3

Views: 986

Answers (1)

wouterw
wouterw

Reputation: 56

The following code might work.

 
    public class MainClass
    {
        public static void Main(string[] args)
        {
            var response = new FileStream("Response.xml", FileMode.Open);

            XDocument doc = XDocument.Load(response);
            XNamespace xmlns = "http://getlocations.ws.hymedis.net";

            var nodes = doc.Descendants(xmlns + "locs")
                                .Elements(xmlns + "loc");

            var list = new List();
            foreach (var node in nodes)
            {
                list.Add(new Location {
                    Name = node.Element(xmlns + "name").Value,
                    Code = node.Element(xmlns + "abbr").Value
                });
            }

            foreach (var item in list) {
                Console.WriteLine(item.Code);
            }
        }

        public class Location 
        {
            public string Code { get; set; }
            public string Name { get; set; }
        }
    }

I don't have much experience with mono but .net made it very easy to consume WCF SOAP services. This article explains how to generate the proxy classes for a WCF service: http://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/

I hope this helps.

Regards, Wouter Willaert

Upvotes: 2

Related Questions