XmlIgnoreAttribute,只序列化过程中使用,而不是在反序列化? [英] XmlIgnoreAttribute, only used during serialization, not during deserialization?

查看:260
本文介绍了XmlIgnoreAttribute,只序列化过程中使用,而不是在反序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是正确理解.NET XmlIgnoreAttribute < /一>,它说:

Am I right in understanding the .NET XmlIgnoreAttribute, which says:

指示的XmlSerializer的Serialize方法不序列化公共字段或公共读/写属性值。

Instructs the Serialize method of the XmlSerializer not to serialize the public field or public read/write property value.

是:

  • 的属性将被反序列化,如果present在XML文件中?
  • 的属性将不会被序列化到一个新的XML文件?

我问的原因是我已经取代了文件中的属性与一个新的提供更多的选择。旧的物业是一个简单的布尔属性,而新的是一个枚举。我已经改变了旧的属性,以便它把新的属性为一个布尔值的值,根据之前我添加了新的东西老物业的意思,get和set已经实施。

The reason I'm asking is that I have replaced a property in the file with a new one with more options. The old property was a simple boolean property, and the new one is an enum. I've changed the old property so that it converts the value of the new property into a boolean value, according to what the old property meant before I added the new one, both get and set has been implemented.

这让我读旧属性,设置新属性静默升级的新文件,并在系列化,新属性加入。

This allowed me to silently upgrade new files by reading the old property, which set the new property, and upon serialization, the new property was added.

不过,我想删除旧的酒店,距离新的XML文件,所以我在想,如果我标记用 [XmlIgnore] ,会老会发生什么XML文件仍然反序列化正常,从文件中读取该属性,或将它完全忽略了?

However, I'd like to remove the old property from new xml files, so I was wondering what would happen if I tagged it with [XmlIgnore], would old xml file still deserialize properly and read that property from the file, or would it be ignored completely?

如果没有,将下面的变化做我想做的?

If not, would the following change do what I want?

[XmlAttribute("is-list")]
[DefaultValue(false)]
public bool IsList
{
    get { return false; }
    set {
        if (value)
            ListHandling = ListHandling.All;
    }
}

这将返回false为所有新对象,因为我已经指定一个默认值,这将被忽略,如果present在一个旧文件,并设置为true,这将改变ListHandling属性,它是新一说是很重要的。

This would return false for all new objects, which would be ignored since I have specified a default value, and if present in an old file, and set to true, that would change the ListHandling property, which is the new one that is important.

<打击> 修改:经测试,我已经验证了这两种方法似乎做我想做的。我会离开的问题,虽然,因为我还是想知道,如果第一个问题上面提到的仅仅是一个实现细节,或者如果文档可以理解的方式。

Edit: Upon testing, I have verified that both approaches seems to do what I want. I'll leave the question though, as I would still like to know if the first behaviour noted above is just an implementation detail, or if the documentation can be understood that way.

推荐答案

如果你使用标记 XmlIgnore 属性,它的忽略的。当XmlSerializer的构建及其序列组装它不考虑。因此,未填充XmlIgnore-d属性都deserialisation期间,并且将剩下的默认值

If you tag a property with XmlIgnore, it is ignored. It is not considered when the XmlSerializer builds its serialisation assembly. Therefore, XmlIgnore-d properties are not populated during deserialisation, and will be left with their default value.

示例程序(代码段编译):

Sample program (for Snippet Compiler):

public static void RunSnippet()
{
  XmlSerializer ser = new XmlSerializer(typeof(Fie));
  Fie f = (Fie)(ser.Deserialize(new StringReader("<Fie><Bob>Hello</Bob></Fie>")));
  WL(f.Bob == null ? "null" : "something");
}

public class Fie
{
  [XmlIgnore]
  public string Bob { get; set; }
}

这是这个程序的输出为(如果你从Fie.Bob删除XmlIgnore输出的东西 )。

The output from this program is null (and if you remove XmlIgnore from Fie.Bob the output is something).

编辑回应您的编辑:这不只是一个实现细节;这的确是属性的记录的行为。从的文档(第一段):如果你申请的XmlIgnoreAttribute一类的任何成员,XmlSerializer的序列化的或反序列化时忽略成员的类的实例。 (强调)

Edit in response to your edit: This is not just an implementation detail; it is indeed the documented behaviour of the attribute. From the Remarks section of the docs (first paragraph): "If you apply the XmlIgnoreAttribute to any member of a class, the XmlSerializer ignores the member when serializing or deserializing an instance of the class." (emphasis added)

这篇关于XmlIgnoreAttribute,只序列化过程中使用,而不是在反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆