将复杂的xml反序列化为对象C#

时间:2018-03-21 04:58:11

标签: c# xml serialization soap

我正在尝试将以下XML响应从Web服务反序列化为C#对象。但不知何故,元素返回null。我在网上看了很多例子,我不知道我哪里弄错了。

 <SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">
   <SOAP-ENV:Header/>
      <SOAP-ENV:Body>
        <ns4:postTransactionResponse xmlns:ns3=\"http://www.domain/cash/api\" xmlns:ns4=\"http://www.domain/cash/api/soap\">
          <ns3:return>
            <field1>200</field1>
            <field2>FIRSTNAME SURNAME</field2>
            <field5>SUBSCRIBER</field5>
            <field6>FIRSTNAME SURNAME</field6>
            <field7>Active</field7>
            <field8>USD</field8>
         </ns3:return>
      </ns4:postTransactionResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我正在使用以下课程

   [XmlRoot("Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
    [DataMember(Name = "Header", Order = 0)]
    public object Header;

    [DataMember(Name = "Body", Order = 1)]
    public EnvelopeBody Body;
}

[XmlRoot("Body", Namespace = "http://www.domain/cash/api/soap")]
public class EnvelopeBody
{
    [DataMember(Name = "postTransactionResponse", Order = 0)]
    public PostTransactionResponse postTransactionResponse;
}

[XmlRoot("postTransactionResponse", Namespace = "http://www.domain/cash/api/soap")]
public class PostTransactionResponse
{
    [DataMember(Name = "return", Order = 0)]
    public ResponseModel return_;
}

[XmlRoot("return", Namespace = "http://www.domain/cash/api")]
public class ResponseModel
{
    [XmlElement("field1")]
    public string field1;

    [XmlElement("field2")]
    public string field2;

    [XmlElement("field3")]
    public string field3 ;

    [XmlElement("field4")]
    public string field4 ;

    [XmlElement("field5")]
    public string field5 ;

    [XmlElement("field6")]
    public string field6 ;

    [XmlElement("field7")]
    public string field7 ;

    [XmlElement("field8")]
    public string field8 ;

    [XmlElement("field9")]
    public string field9 ;

    [XmlElement("field10")]
    public string field10 ;

    [XmlElement("field11")]
    public string field11 ;

    [XmlElement("field12")]
    public string field12 ;

    [XmlElement("field13")]
    public string field13 ;

    [XmlElement("field14")]
    public string field14 ;

    [XmlElement("field15")]
    public string field15 ;
}

这是我用于反序列化XML响应的代码。我正在使用Stream Reader和The Envelope类来获取字段。

            //Getting response from request  
        using (WebResponse Serviceres = request.GetResponse())
        {
            using (StreamReader rd = new StreamReader(Serviceres.GetResponseStream()))
            {

                XmlSerializer sr = new XmlSerializer(typeof(Envelope));
                var obj = (Envelope)sr.Deserialize(rd);
                var dat = obj.Body;
                var un = dat.postTransactionResponse;
                var u = un.return_;  //This is giving null
                var p = u.field2;    //This is giving null
            }
    }

ResponseModel正在提供所有空值。

1 个答案:

答案 0 :(得分:2)

根据您提供的xml,我生成(通过使用xsd.exe)xsd文件(3创建:

e:\>xsd env.xml /c /out:temp\
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.6.1586.0]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'temp\env.xsd'.

之后我根据xsd创建了类:

E:\temp>xsd.exe /c /namespace:SampleNs /language:CS env.xsd env_app1.xsd env_app2.xsd
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.6.1586.0]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'E:\temp\env_env_app1_env_app2.cs'.

将生成的文件添加到项目中,一切正在运行... 有关您的定义有什么问题的信息 - 您可以将生成的类与您的比较(例如,您的主体应该是数组或列表)。

对于外部xml /更复杂的xml我建议总是使用xsd.exe(让你的生活更轻松)。

enter image description here