如何从nusoap服务返回的XML中反序列化对象?

时间:2019-07-12 07:42:18

标签: c# xml xml-deserialization nusoap

我正在尝试从NuSoap服务返回的Xml反序列化对象。 我收到带有WSDL文件的服务,并尝试将其实施到我的项目中。问题在于WSDL文件似乎已损坏。 我可以通过SoapUI访问Web服务,也可以成功将请求手动填写到XML表单中。作为请求,我从服务中获取了Xml。

我已经为某些请求制作了类。我可以序列化并将其成功发布,并获得Xml响应。现在我的实际问题开始了。我想反序列化此响应。

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 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/" xmlns:tns="urn:LicenseManagement">
    <SOAP-ENV:Body>
       <ns1:GetErrorCodesResponse xmlns:ns1="urn:LicenseManagement">
        <RETURN xsi:type="tns:ReturnGetErrorCodes">
          <RC xsi:type="xsd:int">0</RC>
          <DATA xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType=":[55]">
           <item xsi:type="xsd:string">OK</item>
           <item xsi:type="xsd:string">database unavailable</item>
           <item xsi:type="xsd:string">...</item>
          </DATA>
        </RETURN>
       </ns1:GetErrorCodesResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我从响应之一的类的实现开始。 我可以设法反序列化此XML的Envelope,Body和GetErrorResponse部分。当我到达“返回”部分时,我得到一个空值。

我知道我可以将DataSet用于我的问题,但这将需要每个响应的MappingProfile。

try
{
   //
   GetErrorCodesRequestModel request = new GetErrorCodesRequestModel()
   {
     Username = "xxx" ,
     Password = "zzZzZ"
   };
   var res = XmlExtension.SerializeToString(request);

   XmlDocument soapEnvelopeXml = CreateSoapEnvelope(res);

   HttpWebRequest webRequest = CreateWebRequest(command , "POST");
   InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml , webRequest);

   IAsyncResult asyncResult = webRequest.BeginGetResponse(null , null);
   asyncResult.AsyncWaitHandle.WaitOne();

   string soapResponse = string.Empty;
   using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
   using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
   {
      soapResponse = rd.ReadToEnd();
   }

// this is the part where I have to deserialize the soapResponse

   var deserialized = XmlExtension.Deserialize<Envelope>(soapResponse);

// deserialized contains information til RETURN
// important information missing

我的课程看起来像这个atm。

[XmlRoot(ElementName = "RC")]
public class RC
{
   [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
{
   [XmlAttribute(AttributeName = "type" , Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
   public string Type { get; set; }
   [XmlText]
   public string Text { get; set; }
}

[XmlRoot(ElementName = "DATA")]
public class DATA
{
   [XmlElement(ElementName = "item")]
   public Item[] Item { get; set; }
   [XmlAttribute(AttributeName = "type" , Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
   public string Type { get; set; }
   [XmlAttribute(AttributeName = "arrayType" , Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
   public string ArrayType { get; set; }
}

[XmlType(AnonymousType = true , IncludeInSchema = true)]
[XmlRoot(ElementName = "RETURN", Namespace = "tns:ReturnGetErrorCodes" , IsNullable = false)]
public class RETURN
{
   [XmlElement(ElementName = "RC")]
   public RC RC { get; set; }
   [XmlElement(ElementName = "DATA")]
   public DATA DATA { get; set; }
   [XmlAttribute(AttributeName = "type" , Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
   public string Type { get; set; }
}

[XmlRoot(ElementName = "GetErrorCodesResponse" , Namespace = "urn:LicenseManagement")]
public class GetErrorCodesResponse
{
   [XmlElement(ElementName = "RETURN")]
   public RETURN RETURN { 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 = "GetErrorCodesResponse" , Namespace = "urn:LicenseManagement")]
   public GetErrorCodesResponse GetErrorCodesResponse { 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 = "SOAP-ENV" , 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; }

   [XmlAttribute(AttributeName = "SOAP-ENC" , Namespace = "http://www.w3.org/2000/xmlns/")]
   public string SOAPENC { get; set; }

   [XmlAttribute(AttributeName = "tns" , Namespace = "http://www.w3.org/2000/xmlns/")]
   public string Tns { get; set; }
}

1 个答案:

答案 0 :(得分:0)

解决这些问题的最佳方法是使用示例数据创建类并进行序列化。然后将序列化的xml与原始xml进行比较

        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Envelope envelope = new Envelope() {
                Body = new Body() {
                    GetErrorCodesResponse = new GetErrorCodesResponse() {
                        Ns1 = "urn:LicenseManagement",
                        RETURN = new RETURN() {
                            Type = "tns:ReturnGetErrorCodes",
                            RC = new RC() {
                                Type = "xsd:int",
                                Text = "0"
                            },
                            DATA = new DATA() { 
                                Type = "SOAP-ENC:Array",
                                ArrayType = ":[55]",
                                Item = new Item[] {
                                    new Item() { Type = "xsd:string", Text = "OK"},
                                    new Item() { Type = "xsd:string", Text = "database unavailable"},
                                    new Item() { Type = "xsd:string", Text = "..."}
                                }
                            }
                        }
                    }
                }
            };
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME, settings);
            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));

            serializer.Serialize(writer, envelope);
        }
    }