为什么需要 DataContractSerializer 按字母顺序排序的 XML? [英] Why needs DataContractSerializer alphabetically sorted XML?

查看:48
本文介绍了为什么需要 DataContractSerializer 按字母顺序排序的 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据合同:

namespace Wcf.Contracts.Data
{
  [DataContract]
  public class Presence
  {
    [DataMember]
    public int Id { get; set; }

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

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

    [DataMember]
    public TimeSpan? BreakPeriod { get; set; }
  }
}

Presence 的实例序列化为 XML 并将相同的 XML 反序列化回 Presence 的实例效果很好.但是反序列化一个表示序列化的 Presence 对象的字符串变量给了我奇怪的行为.某些属性从 XML 获得默认值而不是指定值.我发现 XML 中表示 Presence 属性的元素必须按字母顺序排列.

Serializing an instance of Presence to XML and deserializing the same XML back to an instance of Presence works well. But deserializing a string variable which represents a serialized Presence object gave me strange behaviors. Some properties got default values rather than the specified values from the XML. I have found out that the elements in the XML which represent the properties of Presence must be alphabetically ordered.

例如在这段代码中

var dcs = new System.Runtime.Serialization.DataContractSerializer(typeof(Wcf.Contracts.Data.Presence));

var xml1 = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Presence xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/Wcf.Contracts.Data"">
  <BreakPeriod>PT30M</BreakPeriod>
  <From>2013-08-21T10:00:00Z</From>
  <To>2013-08-21T15:00:00Z</To>
  <Id>85</Id>
</Presence>";
var xr1 = System.Xml.XmlReader.Create(new System.IO.StringReader(xml1));
var p1 = dcs.ReadObject(xr1) as Wcf.Contracts.Data.Presence;

var xml2 = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Presence xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/Wcf.Contracts.Data"">
  <Id>85</Id>
  <From>2013-08-21T10:00:00Z</From>
  <To>2013-08-21T15:00:00Z</To>
  <BreakPeriod>PT30M</BreakPeriod>
</Presence>";
var xr2 = System.Xml.XmlReader.Create(new System.IO.StringReader(xml2));
var p2 = dcs.ReadObject(xr2) as Wcf.Contracts.Data.Presence;

var xml3 = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Presence xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/Wcf.Contracts.Data"">
  <BreakPeriod>PT30M</BreakPeriod>
  <From>2013-08-21T10:00:00Z</From>
  <Id>85</Id>
  <To>2013-08-21T15:00:00Z</To>
</Presence>";

var xr3 = System.Xml.XmlReader.Create(new System.IO.StringReader(xml3));
var p3 = dcs.ReadObject(xr3) as Wcf.Contracts.Data.Presence;

三个实例都不同.

            | p1                    | p2                         | p3
Id          | default(int) (=0)     | 85                         | 85
From        | 8/21/2013 10:00:00 AM | default(DateTime?) (=null) | 8/21/2013 10:00:00 AM
To          | 8/21/2013  3:00:00 PM | 8/21/2013  3:00:00 PM      | 8/21/2013  3:00:00 PM
BreakPeriod | 00:30:00              | default(TimeSpan?) (=null) | 00:30:00

为什么要对 XML 中的元素进行排序?有人知道为什么 DataContractSerializer 否则无法正确反序列化吗?

Why do the elements in the XML have to be sorted? Does anybody know why the DataContractSerializer otherwise does not deserialize correctly?

推荐答案

这是正常的.数据契约将描述soap 消息的xml 模式.DataContractSerializer 使用任意字母顺序.您可以通过指定来更改顺序:

It's normal. The datacontract will describe the xml schema for the soap message. The DataContractSerializer use arbitrarily alphabetic order. You could change the order by specifying it :

    [DataMember(Order = 1)]
    public int MyProperty { get; set; }

为什么要指定顺序?我没有时间阅读 SOAP RFC,但我认为它是规范化的.

Why does it necessary to specify the order? I don't have time to read the SOAP RFC but I think it's normalize.

如果我们仔细想想,这是速度和尺寸优化的逻辑.DataContract 通过不为 null 属性值编写任何 xml 来指定 null.想象一下,如果您有一个具有 200 个属性的对象,您必须读取所有 xml 以确定该属性是否为空.如果你有一个 xml 模式来对元素进行排序,它会更快.

And if we think about it, it's logic for speed and size optimization. DataContract specify the null by not writing any xml for null property value. And imagine if you have an object with 200 properties you have to read all the xml to determine if the property is null. If you have an xml schema that ordered element it's faster.

我希望我所说的有助于更好地理解.

I hope that what I have had to say has contributed to a better understanding.

这篇关于为什么需要 DataContractSerializer 按字母顺序排序的 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆