Десериализовать SOAP-ответ - PullRequest
0 голосов
/ 29 июня 2018

Данные возвращаются из веб-службы Apache, поэтому я не могу использовать Add Service Reference, так как сгенерированные классы не могут выполнить вызов веб-службы или вернуть данные

Я пытаюсь десериализовать приведенный ниже ответ SOAP в C # .NET. Я могу подтвердить, что оператор XDocument правильно извлекает только фактический ответ, который я хочу десериализовать (взятый из этого ответа ), однако, когда я проверяю объект ответа, и Attribute1, и список MapItem являются null.

Любая помощь будет принята с благодарностью.

SOAP 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>
  <ns1:checkcompanyResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
   <checkcompanyReturn xsi:type="ns2:Map" xmlns:ns2="http://xml.apache.org/xml-soap">
    <item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
     <key xsi:type="soapenc:string">Entry1</key>
     <value xsi:type="soapenc:string">Y</value>
    </item>
    <item>
     <key xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Entry2</key>
     <value xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">N</value>
    </item>
   </checkcompanyReturn>
  </ns1:checkcompanyResponse>
 </soapenv:Body>
</soapenv:Envelope>

C # Классы

[XmlRoot(ElementName = "checkcompanyResponse", Namespace = "http://DefaultNamespace")]
public class CheckCompanyResponse {
    [XmlElement("attribute1")]
    public string Attribute1 { get; set; }
    [XmlArray("checkcompanyReturn")]
    [XmlArrayItem("item")]
    public List<MapItem> Map { get; set; }
}

public class MapItem {
    public string key { get; set; }
    public string value { get; set; }
}

код десериализации

string fileName = @"C:\Users\SLUKY\Desktop\test.xml";
XDocument document = XDocument.Load(fileName);
XName soapBody = XName.Get("Body", "http://schemas.xmlsoap.org/soap/envelope/");
XmlSerializer xmlSerializer = new XmlSerializer(typeof(CheckCompanyResponse));
CheckCompanyResponse response = (CheckCompanyResponse)xmlSerializer.Deserialize(document.Descendants(soapBody)
    .First()
    .FirstNode
    .CreateReader()
);

1 Ответ

0 голосов
/ 29 июня 2018

Пожалуйста, используйте эту модель

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace yournamespace
{
    [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; }
        [XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Soapenc { 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; }
        [XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Soapenc { get; set; }
    }

    [XmlRoot(ElementName="item")]
    public class Item {
        [XmlElement(ElementName="key")]
        public Key Key { get; set; }
        [XmlElement(ElementName="value")]
        public Value Value { get; set; }
        [XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Soapenc { get; set; }
    }

    [XmlRoot(ElementName="checkcompanyReturn")]
    public class CheckcompanyReturn {
        [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; }
        [XmlAttribute(AttributeName="ns2", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Ns2 { get; set; }
    }

    [XmlRoot(ElementName="checkcompanyResponse", Namespace="http://DefaultNamespace")]
    public class CheckcompanyResponse {
        [XmlElement(ElementName="checkcompanyReturn")]
        public CheckcompanyReturn CheckcompanyReturn { get; set; }
        [XmlAttribute(AttributeName="encodingStyle", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
        public string EncodingStyle { get; set; }
        [XmlAttribute(AttributeName="ns1", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Ns1 { get; set; }
    }

    [XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body {
        [XmlElement(ElementName="checkcompanyResponse", Namespace="http://DefaultNamespace")]
        public CheckcompanyResponse CheckcompanyResponse { 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="soapenv", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Soapenv { 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; }
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...