简体   繁体   English

XmlIgnoreAttribute,仅在序列化期间使用,而不是在反序列化期间使用?

[英]XmlIgnoreAttribute, only used during serialization, not during deserialization?

Am I right in understanding the .NET XmlIgnoreAttribute , which says: 我是否正确理解.NET XmlIgnoreAttribute ,它说:

Instructs the Serialize method of the XmlSerializer not to serialize the public field or public read/write property value. 指示XmlSerializer的Serialize方法不要序列化公共字段或公共读/写属性值。

that: 那:

  • The property will be deserialized, if present in the XML file? 如果存在于XML文件中,该属性将被反序列化?
  • The property will not be serialized to a new XML file? 该属性不会被序列化为新的XML文件?

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. 我已经更改了旧属性,以便将新属性的值转换为布尔值,根据旧属性在添加新属性之前的含义,get和set都已实现。

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. 这允许我通过读取设置新属性的旧属性来静默升级新文件,并在序列化时添加新属性。

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? 但是,我想从新的xml文件中删除旧属性,所以我想知道如果我用[XmlIgnore]标记它会发生什么,旧的xml文件是否仍然正确地反序列化并从文件中读取该属性,或者它是否会完全被忽略?

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;
    }
}

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. 对于所有新对象,这将返回false,因为我已经指定了默认值,并且如果存在于旧文件中,并且设置为true,则会更改ListHandling属性,这将是重要的新属性。

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. 我会留下这个问题,因为我仍然想知道上面提到的第一个行为是否只是一个实现细节,或者是否可以通过这种方式理解文档。

If you tag a property with XmlIgnore , it is ignored . 如果使用XmlIgnore标记属性,则会将其忽略 It is not considered when the XmlSerializer builds its serialisation assembly. XmlSerializer构建其序列化程序集时不考虑它。 Therefore, XmlIgnore-d properties are not populated during deserialisation, and will be left with their default value. 因此,在反序列化期间不会填充XmlIgnore-d属性,并将保留其默认值。

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; }
}

The output from this program is null (and if you remove XmlIgnore from Fie.Bob the output is something ). 该程序的输出是null (如果你从Fie.Bob删除XmlIgnore输出something )。

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." 文档的备注部分(第一段):“如果将XmlIgnoreAttribute应用于类的任何成员,则XmlSerializer在序列化或反序列化类的实例时会忽略该成员。” (emphasis added) (重点补充)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM