在C#中反序列化SOAP XML PHP响应

时间:2019-03-05 08:52:58

标签: c# php xml soap

我在想办法从C#客户端的PHP Web服务反序列化XML响应时遇到麻烦。响应采用以下格式:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:idaWS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:authenticateResponse>
<Result xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">status</key>
<value xsi:type="xsd:boolean">true</value>
</item>
<item>
<key xsi:type="xsd:string">token</key>
<value xsi:type="xsd:string">#####################</value>
</item>
<item>
<key xsi:type="xsd:string">message</key>
<value xsi:type="xsd:string">Authentication ok : ##########</value>
</item>
</Result>
</ns1:authenticateResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我必须使用PHP连接到外部Web服务。 在我的解决方案中,必须使用C#连接到此Web服务。 我最好的结果是 Envelop> Body> authenticateResponse>结果= NULL 。  我不知道该怎么做。 生成的模型

[XmlRoot(ElementName = "key")]
    public class Key
    {
        [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "value")]
    public class Value
    {
        [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "item")]
    public class Item
    {
        [XmlElement(ElementName = "key")]
        public Key Key { get; set; }
        [XmlElement(ElementName = "value")]
        public Value Value { get; set; }
    }

    [XmlRoot(ElementName = "Result",Namespace = "http://xml.apache.org/xml-soap")]
    public class Result
    {
        [XmlElement(ElementName = "item")]
        public List<Item> Item { get; set; }
        [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
    }

    [XmlRoot(ElementName = "authenticateResponse", Namespace = "urn:idaWS")]
    public class AuthenticateResponse
    {
        [XmlElement(ElementName = "Result", Namespace = "http://xml.apache.org/xml-soap")]
        public Result Result { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "authenticateResponse", Namespace = "urn:idaWS")]
        public AuthenticateResponse AuthenticateResponse { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "encodingStyle", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public string EncodingStyle { get; set; }
        [XmlAttribute(AttributeName = "SOAP-ENC", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string SOAPENC { get; set; }
        [XmlAttribute(AttributeName = "ns2", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Ns2 { get; set; }
        [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsd { get; set; }
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName = "ns1", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Ns1 { get; set; }
        [XmlAttribute(AttributeName = "SOAP-ENV", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string SOAPENV { get; set; }
    }

我尝试了一些示例,但是没有用

using(WebResponse response = request.GetResponse()) {
    using(StreamReader rd = new StreamReader(response.GetResponseStream())) {
        string soapResult = rd.ReadToEnd();


        try {
            Envelope en = new Envelope();
            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
            using(TextReader reader = new StringReader(soapResult)) {
                en = (Envelope) serializer.Deserialize(reader);
            }
        } catch (Exception e) {
            Console.WriteLine("1 :" + e.Message);
        }
        try {

            var document = XDocument.Parse(soapResult);
            var results = document.Descendants()
                .Where(d => d.Name.LocalName == "Envelope")
                .Select(d => d.ToObject < Envelope > ()).ToList();
        } catch (Exception e) {
            Console.WriteLine("2 :" + e.Message);
        }


        try {
            var stream = new System.IO.MemoryStream();
            var writer = new System.IO.StreamWriter(stream);
            writer.Write(soapResult);
            writer.Flush();
            stream.Position = 0;

            var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(Envelope));
            var obj = serializer.ReadObject(stream);
        } catch (Exception e) {
            Console.WriteLine("3 :" + e.Message);
        }

        try {
            AuthenticateResponse ar = Class2.SOAPToObject < AuthenticateResponse > (soapResult);
        } catch (Exception e) {
            Console.WriteLine("4 :" + e.Message);
        }

        try {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(soapResult); //loading soap message as string
            XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);

            xmlNamespaceManager.AddNamespace("ns1", "urn:idaWS");
            xmlNamespaceManager.AddNamespace("ns2", "http://xml.apache.org/xml-soap");

            XmlNodeList xmlNodeList = xmlDocument.SelectNodes("//ns1:authenticateResponse/Result/", xmlNamespaceManager);

            string[] results = new string[xmlNodeList.Count];

            var str = "";
            var count = 0;
            foreach(XmlNode xmlNode in xmlNodeList) {
                str += xmlNode.InnerText;
            }

        } catch (Exception e) {
            Console.WriteLine("5 :" + e.Message);
        }

        try {
            var document = XDocument.Parse(soapResult);
            var xmlSerializer = new XmlSerializer(typeof(AuthenticateResponse));
            var soapBody = XName.Get("Body", "http://xml.apache.org/xml-soap");
            var test = document.Descendants(soapBody);
            // Exception
            var tokenInfo = (AuthenticateResponse) xmlSerializer.Deserialize(test.First()
                .FirstNode
                .CreateReader());
        } catch (Exception e) {
            Console.WriteLine("6 :" + e.Message);
        }

        try {
            SoapReflectionImporter importer = new SoapReflectionImporter(new SoapAttributeOverrides(), "urn:idaWS");
            XmlTypeMapping mapp = importer.ImportTypeMapping(typeof(AuthenticateResponse));
            XmlSerializer xmlSerializer2 = new XmlSerializer(mapp); //typeof(T), xr
            using(TextReader sr = new StringReader(soapResult)) {
                var o = (AuthenticateResponse) xmlSerializer2.Deserialize(sr);
            }
        } catch (Exception e) {
            Console.WriteLine("7 :" + e.Message);
        }
        try {
            var document = XDocument.Parse(soapResult);
            var results = document.Descendants()
                .Where(d => d.Name.LocalName == "authenticateResponse")
                .Select(d => d.ToObject < AuthenticateResponse > ())
                .ToList();
        } catch (Exception e) {
            Console.WriteLine("8 :" + e.Message);
        }
        try {
            XDocument doc = XDocument.Parse(soapResult);

            XNamespace metadataNameSpace = "urn:idaWS";
            XNamespace headNameSpace = "http://xml.apache.org/xml-soap";

            //if you want tags providerDemographics
            var list = (from d in doc.Descendants(headNameSpace + "Result") select new {
                item = d.Element(headNameSpace + "item").Value
            }).ToList();

            //if you want metadata tags data   
            var metaDatalist = (from d in doc.Descendants(metadataNameSpace + "authenticateResponse") select d).ToList();
        } catch (Exception e) {
            Console.WriteLine("9 :" + e.Message);
        }

        ///factory 

        Console.WriteLine(soapResult);
    }
}

0 个答案:

没有答案