XML deserialization ignores properties out of alphabetical order

后端 未结 1 1868
天涯浪人
天涯浪人 2021-01-25 14:14

I have small problem - XML deserialization completely ignores items, which are out of alphabetic order. In example object (description in end of question), Birthday

相关标签:
1条回答
  • 2021-01-25 14:57

    WCF uses DataContractSerializer. This serializer is sensitive to XML element order, see Data Member Order. There's no quick way to disable this, instead you need to replace the serializer with XmlSerializer.

    To do this, see Using the XmlSerializer Class, then and apply [XmlSerializerFormat] to your service, for instance:

    [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; }
    }
    
    0 讨论(0)
提交回复
热议问题