【问题标题】:XML deserialization ignores properties out of alphabetical orderXML 反序列化忽略不按字母顺序排列的属性
【发布时间】:2016-06-16 20:16:42
【问题描述】:

我有一个小问题 - XML 反序列化完全忽略不按字母顺序排列的项目。在示例对象(问题末尾的描述)中,Birthday 节点在FirstName 节点之后,反序列化后它被忽略并分配默认值。任何其他类型和名称都相同(我在 PatientInfo 类型的节点 Patient 之后有 Guid 类型的节点 CaseId 类型,反序列化后它具有默认值)。

我正在使用下一个代码在一个应用程序中对其进行序列化:

public static string SerializeToString(object data)
{
    if (data == null) return null;
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("", "");

    // what should the XmlWriter do?
    var settings = new XmlWriterSettings
    {
        OmitXmlDeclaration = true,
        NewLineChars = ""
    };

    using (var stringwriter = new System.IO.StringWriter())
    {
        // Use an XmlWriter to wrap the StringWriter
        using (var xmlWriter = XmlWriter.Create(stringwriter, settings))
        {
            var serializer = new XmlSerializer(data.GetType(), "");
            // serialize to the XmlWriter instance
            serializer.Serialize(xmlWriter, data, ns);
            return stringwriter.ToString();
        }
    }
}

这种方法用于获得正确的结果作为 WebMethod 的参数(完整的问题描述为here)。结果是这样的:

Foo2015-12-19T16:21:48.4009949+01:0000000000-0000-0000-0000-000000000000 00000000-0000-0000-0000-000000000000

我也在以简单的方式在另一个应用程序中反序列化它

public static T Deserialize<T>(string xmlText)
{
    if (String.IsNullOrEmpty(xmlText)) return default(T);
    using (var stringReader = new StringReader(xmlText))
    {
        var serializer = new XmlSerializer(typeof(T));
        return (T)serializer.Deserialize(stringReader);
    }
}

示例对象:

[XmlRoot("PatientInfo")]
public class PatientInfo
{
    [XmlElement("FirstName")]
    public string FirstName { get; set; }
    [XmlElement("LastName")]
    public string LastName { get; set; }
    [XmlElement("SSN")]
    public string SSN { get; set; }
    [XmlElement("Birthday")]
    public DateTime? Birthday { get; set; }
    [XmlElement("RequestedClientID")]
    public Guid RequestedClientID { get; set; }
    [XmlElement("patientId")]
    public Guid patientId { get; set; }
}

所以,我想回答两个问题之一 - 1) 如何调整我的序列化以使所有项目按字母顺序排列? 2) 如何调整我的反序列化,使其不会忽略不按字母顺序排列的项目?

感谢任何帮助。

更新:

刚刚发现,我正在使用的反序列化方法实际上根本没有用于我的问题,因为我将序列化信息用作 WebMethod 的数据,并且它是通过 WCF 的一些内部机制进行反序列化的。

【问题讨论】:

  • 看来,这种简单的方法是将对象类中的所有属性按顺序排列,但我会很恼火地为我添加到类中的每个新属性搜索适当的位置。
  • 我无法重现您的问题。如果我颠倒 XML 中节点的顺序,它仍然会成功反序列化。见dotnetfiddle.net/xN5PSk
  • @dbc 实际上,我似乎做了一个错误的陈述,而且它与 Web Method 一起使用这一事实比我想象的要重要得多。实际上,第二个应用程序在Web Method中接收数据,并通过某种内部机制对其进行反序列化,因此我的反序列化方法与此无关。抱歉造谣=(

标签: c# xml wcf serialization xml-deserialization


【解决方案1】:

WCF uses DataContractSerializer。此序列化程序对 XML 元素顺序很敏感,请参阅 Data Member Orderdisable this 没有快捷方式,您需要将序列化程序替换为 XmlSerializer

为此,请参阅Using the XmlSerializer Class,然后将[XmlSerializerFormat] 应用于您的服务,例如:

[ServiceContract]
[XmlSerializerFormat]
public interface IPatientInfoService
{
    [OperationContract]
    public void ProcessPatientInfo(PatientInfo patient)
    {
        // Code not shown.
    }
}

[XmlRoot("PatientInfo")]
public class PatientInfo
{
    [XmlElement("FirstName")]
    public string FirstName { get; set; }
    [XmlElement("LastName")]
    public string LastName { get; set; }
    [XmlElement("SSN")]
    public string SSN { get; set; }
    [XmlElement("Birthday")]
    public DateTime? Birthday { get; set; }
    [XmlElement("RequestedClientID")]
    public Guid RequestedClientID { get; set; }
    [XmlElement("patientId")]
    public Guid patientId { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2014-05-21
    • 1970-01-01
    • 2018-01-14
    相关资源
    最近更新 更多