XML 反序列化忽略不按字母顺序排列的属性 [英] XML deserialization ignores properties out of alphabetical order
问题描述
我有一个小问题 - XML 反序列化完全忽略了不按字母顺序排列的项目.在示例对象(问题末尾的描述)中,Birthday
节点在FirstName
节点之后,反序列化后将被忽略并分配默认值.对于任何其他类型和名称都相同(我在 PatientInfo
类型的节点 Patient
之后有 Guid
类型的节点 CaseId
,反序列化后有默认值).
I have small problem - XML deserialization completely ignores items, which are out of alphabetic order. In example object (description in end of question), Birthday
node is after FirstName
node, and it is ignored and assigned default value after deserialization. Same for any other types and names (I had node CaseId
of Guid
type after node Patient
of PatientInfo
type, and after deserialization it had default value).
我在一个应用程序中序列化它,使用下一个代码:
I'm serializing it in one application, using next code:
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 的参数(在此处描述了完整的问题).结果是这样的:
Such approach was used to get proper result as argument for WebMethod (full problem described here). Results are something like this:
<PatientInfo><FirstName>Foo</FirstName><Birthday>2015-12-19T16:21:48.4009949+01:00</Birthday><RequestedClientID>00000000-0000-0000-0000-000000000000</RequestedClientID>00000000-0000-0000-0000-000000000000</patientId></PatientInfo>
我也在另一个应用程序中以简单的方式反序列化它
Also I'm deserializing it in another application in simple manner
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) 如何调整我的反序列化,使其不会按字母顺序忽略项目?
So, I'd like to have answer for one of two questions - 1) How can I adjust my serialization to have all items in alphabetical order? 2) How can I adjust my deserialization, so it won't ignore items out of alphabetical order?
感谢任何帮助.
更新:
刚刚发现,我正在使用的反序列化方法实际上根本没有在我的问题中使用,因为我使用序列化信息作为 WebMethod 的数据,并且它是通过 WCF 的一些内部机制反序列化的.
Just figured out, that deserialization method I'm using is not actually used at all in my problem, since I'm using serialized info as data with WebMethod, and it is deserialized with some internal mechanism of WCF.
推荐答案
WCF 使用 DataContractSerializer
.此序列化程序对 XML 元素顺序很敏感,请参阅 Data会员订单.没有快速的方法来禁用此,您需要将序列化程序替换为 XmlSerializer
.
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
.
为此,请参阅使用 XmlSerializer类,然后应用[XmlSerializerFormat]
到您的服务,例如:
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; }
}
这篇关于XML 反序列化忽略不按字母顺序排列的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!