2016-03-03 78 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; } 
}