数据契约序列化不适用于所有元素

发布于 2024-08-26 08:17:50 字数 1700 浏览 4 评论 0原文

我有一个 XML 文件,我正在尝试将其序列化为一个对象。有些元素被忽略了。

我的 XML 文件:

<?xml version="1.0" encoding="utf-8" ?> 
<License xmlns="http://schemas.datacontract.org/2004/07/MyApp.Domain">
<Guid>7FF07F74-CD5F-4369-8FC7-9BF50274A8E8</Guid> 
<Url>http://www.gmail.com</Url> 
<ValidKey>true</ValidKey> 
<CurrentDate>3/1/2010 9:39:28 PM</CurrentDate> 
<RegistrationDate>3/8/2010 9:39:28 PM</RegistrationDate> 
<ExpirationDate>3/8/2099 9:39:28 PM</ExpirationDate> 
</License>

我的类定义:

[DataContract]
public class License
{
    [DataMember]
    public virtual int Id { get; set; }
    [DataMember]
    public virtual string Guid { get; set; }
    [DataMember]
    public virtual string ValidKey { get; set; }
    [DataMember]
    public virtual string Url { get; set; }
    [DataMember]
    public virtual string CurrentDate { get; set; }
    [DataMember]
    public virtual string RegistrationDate { get; set; }
    [DataMember]
    public virtual string ExpirationDate { get; set; }
}

我的序列化尝试:

XmlDocument Xmldoc = new XmlDocument();
Xmldoc.Load(string.Format(url));

string xml = Xmldoc.InnerXml;
var serializer = new DataContractSerializer(typeof(License));
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
License license = (License)serializer.ReadObject(memoryStream);
memoryStream.Close();

以下元素已序列化:

  • Guid
  • ValidKey

以下元素未序列化:

  • Url
  • CurrentDate
  • RegistrationDate
  • ExpirationDate

用“blah”替换 xml 文件中的字符串日期也不起作用。什么给?

I have an XML file that I'm trying to serialize into an object. Some elements are being ignored.

My XML File:

<?xml version="1.0" encoding="utf-8" ?> 
<License xmlns="http://schemas.datacontract.org/2004/07/MyApp.Domain">
<Guid>7FF07F74-CD5F-4369-8FC7-9BF50274A8E8</Guid> 
<Url>http://www.gmail.com</Url> 
<ValidKey>true</ValidKey> 
<CurrentDate>3/1/2010 9:39:28 PM</CurrentDate> 
<RegistrationDate>3/8/2010 9:39:28 PM</RegistrationDate> 
<ExpirationDate>3/8/2099 9:39:28 PM</ExpirationDate> 
</License>

My class definition:

[DataContract]
public class License
{
    [DataMember]
    public virtual int Id { get; set; }
    [DataMember]
    public virtual string Guid { get; set; }
    [DataMember]
    public virtual string ValidKey { get; set; }
    [DataMember]
    public virtual string Url { get; set; }
    [DataMember]
    public virtual string CurrentDate { get; set; }
    [DataMember]
    public virtual string RegistrationDate { get; set; }
    [DataMember]
    public virtual string ExpirationDate { get; set; }
}

And my Serialization attempt:

XmlDocument Xmldoc = new XmlDocument();
Xmldoc.Load(string.Format(url));

string xml = Xmldoc.InnerXml;
var serializer = new DataContractSerializer(typeof(License));
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
License license = (License)serializer.ReadObject(memoryStream);
memoryStream.Close();

The following elements are serialized:

  • Guid
  • ValidKey

The following elements are not serialized:

  • Url
  • CurrentDate
  • RegistrationDate
  • ExpirationDate

Replacing the string dates in the xml file with "blah" doesn't work either. What gives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-09-02 08:17:50

DataContractSerializer 要求表示属性的 XML 元素按字母顺序排列。因此,您的 XML 应该是:

<?xml version="1.0" encoding="utf-8" ?> 
<License xmlns="http://schemas.datacontract.org/2004/07/MyApp.Domain">
    <CurrentDate>3/1/2010 9:39:28 PM</CurrentDate> 
    <ExpirationDate>3/8/2099 9:39:28 PM</ExpirationDate> 
    <Guid>7FF07F74-CD5F-4369-8FC7-9BF50274A8E8</Guid> 
    <RegistrationDate>3/8/2010 9:39:28 PM</RegistrationDate> 
    <Url>http://www.gmail.com</Url> 
    <ValidKey>true</ValidKey> 
</License>

正如 John 指出的,例外情况是您在 DataMember 属性上使用 Order 属性。在这种情况下,XML 元素必须按指定的顺序排列。

DataContractSerializer requires the XML elements representing properties to be in alphabetical order. So, your XML should be:

<?xml version="1.0" encoding="utf-8" ?> 
<License xmlns="http://schemas.datacontract.org/2004/07/MyApp.Domain">
    <CurrentDate>3/1/2010 9:39:28 PM</CurrentDate> 
    <ExpirationDate>3/8/2099 9:39:28 PM</ExpirationDate> 
    <Guid>7FF07F74-CD5F-4369-8FC7-9BF50274A8E8</Guid> 
    <RegistrationDate>3/8/2010 9:39:28 PM</RegistrationDate> 
    <Url>http://www.gmail.com</Url> 
    <ValidKey>true</ValidKey> 
</License>

The exception, as John pointed out, is if you are using the Order property on your DataMember attributes. In that case, the XML elements must be in the specified order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文