【问题标题】:Deserialize Complex xml to Object C#将复杂 xml 反序列化为对象 C#
【发布时间】:2018-03-21 04:58:11
【问题描述】:

我正在尝试将以下 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 和 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 给出所有空值。

【问题讨论】:

    标签: c# xml serialization soap


    【解决方案1】:

    根据您提供的 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(让您的生活更轻松)。

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      相关资源
      最近更新 更多