monodevelop解析肥皂反应

时间:2012-04-25 16:14:43

标签: c# soap monodevelop

现在已经阅读了这个和其他论坛几个小时和几天,无法找到我的肥皂响应的解决方案。在这里尝试各种答案,但无法解析我的回答:(

我的回复:

<?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>

我的c#代码到目前为止(param soapresponse是整个soapresponse的字符串格式)我的回答是正确的,所以完整的xml soap响应,但是不能解析好

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);
        }

    }

但每次我的list.count返回0 - &gt;所以没有数据.. plz帮助我!

1 个答案:

答案 0 :(得分:2)

以下代码可能有用。

 
    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; }
        }
    }

我没有太多使用mono的经验,但.net使得使用WCF SOAP服务非常容易。 本文介绍如何为WCF服务生成代理类: http://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/

我希望这会有所帮助。

此致 Wouter Willaert