2016-03-03 79 views
0

我有一个小问题 - XML反序列化完全忽略了不符合字母顺序的项目。在示例对象(问题结尾的描述)中,Birthday节点在FirstName节点之后,并且在反序列化之后它被忽略并分配默认值。同样的任何其他类型和名称(我节点CaseIdGuid类型节点PatientPatientInfo类型,并在反序列化后它有默认值)。XML反序列化忽略了字母顺序以外的属性

我序列化它在一个应用程序,使用下面的代码:

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)。结果是这样的:

< PatientInfo> <姓>富< /姓> <生日> 2015-12-19T16:21:48.4009949 + 01:00 < /生日> < RequestedClientID> 00000000-0000- 0000-0000-000000000000 </RequestedClientID> 00000000-0000-0000-0000-000000000000 </patientId> </PatientInfo>

另外我以简单的方式

反序列化它在另一个应用程序
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的一些内部机制。

+0

看来,这么简单的方法就是把在对象类的所有属性才能,但我会相当恼火地为我添加到课堂上的每一个新房产搜寻适当的地方。 – lentinant

+0

我无法重现您的问题。如果我颠倒了XML中节点的顺序,它仍然成功地反序列化。请参阅https://dotnetfiddle.net/xN5PSk – dbc

+0

@dbc其实,看起来,我做了一个错误的陈述,而且这个与Web Method一起使用的事实远比我想的要重要得多。实际上,第二个应用程序在Web Method中接收数据,并且使用一些内部机制进行反序列化,所以我的反序列化方法与它无关。抱歉,造谣=( – lentinant

回答

1

WCF uses DataContractSerializer。该序列化程序对XML元素顺序很敏感,请参阅Data Member Order。有没有快速的方法,而你需要用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; } 
}