DataContractSerializer无法反序列化Net Core 2

时间:2018-02-28 00:02:04

标签: .net-core asp.net-core-2.0 asp.net-core-webapi datacontractserializer coreclr

我正在尝试使用.NET Core 2中的DataContractSerializer将xml反序列化为已知对象。

这是我的xml。 (我无法控制xml。这就是我如何将XML作为对某些api方法的响应)。出于测试目的,我已经捕获了xml并放入了xml文件。

<Response>
  <ClientID>TestClient</ClientID>
  <FileName>E:\MyData\20180223084535390PM.xml</FileName>
  <UploadStatus>Succeeded</UploadStatus>
  <UploadMessage>Imported Successfully</UploadMessage>
  <ConfirmationNumber>0abcb25f2675</ConfirmationNumber>
  <ImportTime>2018-02-23T15:48:01.887</ImportTime>
  <StartTime>2018-02-23T15:48:03.113</StartTime>
  <EndTime>2018-02-23T15:53:14.76</EndTime>
  <Count>6</Count>
  <Amount>3446.3500</Amount>
  <Messages />
</Response>

我对应的C#对象

[DataContract(Name = "Response", Namespace = "")]
public class MyResponse
{
    [DataMember]
    public string ClientID { get; set; }

    [DataMember]
    public string FileName { get; set; }

    [DataMember]
    public string UploadStatus { get; set; }

    [DataMember]
    public string UploadMessage { get; set; }

    [DataMember]
    public string ConfirmationNumber { get; set; }

    [DataMember]
    public DateTime? ImportTime { get; set; }

    [DataMember]
    public DateTime? StartTime { get; set; }

    [DataMember]
    public DateTime? EndTime { get; set; }

    [DataMember]
    public int? Count { get; set; }

    [DataMember]
    public decimal? Amount { get; set; }

    [DataMember]
    public string Messages { get; set; }    
}

反序列化代码

    [Fact]
    public void TestDeSerialize()
    {
        var file = AppDomain.CurrentDomain.BaseDirectory + "Data\\test.xml";
        var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(MyResponse));
        MyResponse result = null;
        using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
        {
            result = (MyResponse)serializer.ReadObject(fs);
        }
    }

但是,当它将xml反序列化为对象时,即使它们在XML文件中具有相应的值,大多数对象属性也是NULL。请参阅下面的quickwatch。

enter image description here

我不确定我在这里失踪了什么?

更新1
请注意,当我分别用DataContractDataMember替换XmlRootXmlElement属性,然后使用XmlSerializer对xml进行反序列化时,生成的对象具有填充了所有属性。 因此,它适用于XmlSerializer,但不适用于DataContractSerializer 我想使用DataContractSerializer

1 个答案:

答案 0 :(得分:1)

对于 DataContractSerializer ,默认使用按字母排序;并期望XML元素按顺序到达;通常会丢弃乱序元素 似乎收到了XML,并且您无法更改XML元素顺序,您可以使用订单属性,如下所示。

[DataContract(Name = "Response", Namespace = "")]
public class MyResponse
{
    [DataMember(Order =1)]
    public string ClientID { get; set; }
    [DataMember(Order = 2)]
    public string FileName { get; set; }
    [DataMember(Order = 3)]
    public string UploadStatus { get; set; }
    [DataMember(Order = 4)]
    public string UploadMessage { get; set; }
    [DataMember(Order = 5)]
    public string ConfirmationNumber { get; set; }
    [DataMember(Order = 6)]
    public DateTime? ImportTime { get; set; }
    [DataMember(Order = 7)]
    public DateTime? StartTime { get; set; }
    [DataMember(Order = 8)]
    public DateTime? EndTime { get; set; }
    [DataMember(Order = 9)]
    public int? Count { get; set; }
    [DataMember(Order = 10)]
    public decimal? Amount { get; set; }
    [DataMember(Order = 11)]
    public string Messages { get; set; }
}