C# 为什么我的公共属性没有被 XmlSerializer 序列化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/575432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why isn't my public property serialized by the XmlSerializer?
提问by Rory
This is one i struggled with for ages so thought I'd document somewhere. (Apologies for asking and answering a question.)
这是我多年来一直在努力的一个,所以我想我会在某个地方记录下来。(对提问和回答问题表示歉意。)
(C# .net 2.0) I had a class that was being serialized by XmlSerializer, I added a new public property however it wasn't being included in the output XML.
(C# .net 2.0)我有一个由 XmlSerializer 序列化的类,我添加了一个新的公共属性,但它没有包含在输出 XML 中。
It's not mentioned in the docs anywhere I could find, but public properties must have a set as well as a get to be serialized! I guess this is because it assumes that if you're going to serialize then you'll want to deserialize from the same file, so only serializes properties that have both a set and a get.
在我能找到的任何地方的文档中都没有提到它,但是公共属性必须有一个 set 以及一个要序列化的 get !我猜这是因为它假设如果您要序列化,那么您将希望从同一个文件中反序列化,因此只序列化具有 set 和 get 的属性。
采纳答案by Marc Gravell
As mentioned, most properties must have both a getter and setter; the main exception to this is lists - for example:
如前所述,大多数属性必须同时具有 getter 和 setter;对此的主要例外是列表 - 例如:
private readonly List<Foo> bar = new List<Foo>();
public List<Foo> Bar {get { return bar; } } // works fine
which will work fine; however, if XmlSerializer
findsa setter - it demands that it is public; the following will notwork:
这将正常工作;但是,如果XmlSerializer
找到一个 setter - 它要求它是公开的;以下将无法正常工作:
public List<Foo> Bar {get; private set;} // FAIL
Other reasons it might not serialize:
它可能无法序列化的其他原因:
- it isn't public with get and set (or is
readonly
for a field) - it has a
[DefaultValue]
attribute, and is with that value - it has a public
bool ShouldSerializeFoo()
method that returned false - it has a public
bool FooSpecified {get;set;}
property or field that returned false - it is marked
[XmlIgnore]
- it is marked
[Obsolete]
- 它不是公开的 get 和 set (或者是
readonly
一个字段) - 它有一个
[DefaultValue]
属性,并且具有该值 - 它有一个
bool ShouldSerializeFoo()
返回 false的公共方法 - 它有一个
bool FooSpecified {get;set;}
返回 false的公共属性或字段 - 它被标记
[XmlIgnore]
- 它被标记
[Obsolete]
Any of these will cause it not to serialize
任何这些都会导致它不序列化
回答by Cheeso
The point about getter+setter is made in the 3rd paragraph on the "Intro to Xml Serialization" page. It's actually in a call-out box. Can't miss it!
关于 getter+setter 的观点在“ Xml 序列化简介”页面的第 3 段中提出。它实际上是在一个呼出框。不能错过!
Intro-to-XML Serialization http://www.freeimagehosting.net/uploads/2f04fea2db.png
XML 序列化简介 http://www.freeimagehosting.net/uploads/2f04fea2db.png
(having a little too much fun with Freeimagehosting.net)
(在 Freeimagehosting.net 上玩得太开心了)
回答by anonymous
Also properties that return null are not serialized!
返回 null 的属性也不会序列化!
回答by Nanda
And if your class inherits a list and also has its own members, only the elements of the list get serialized. The data present in your class members is not captured. Took some time figuring out this!
如果你的类继承了一个列表并且也有它自己的成员,那么只有列表的元素会被序列化。不会捕获类成员中存在的数据。花了一些时间弄清楚这一点!
回答by JonSchn
if you don't want to implement proper Setters (because maybe you are neither wanting to deserialize or change an objects value) you can just use dummy setters like this set { }
, so that the XMLSerializer
works, but nothing happens if you use the Setter...
如果您不想实现正确的 Setter(因为也许您既不想反序列化也不想更改对象值),您可以使用像这样的虚拟 setter set { }
,这样可以XMLSerializer
工作,但如果您使用 Setter,则不会发生任何事情...
i.E.
IE
public string ID { get { return _item.ID.ToString(); } set { } }
回答by NobodysNightmare
One more thing to add about serialization of collections:
关于集合的序列化还有一件事要添加:
The XmlSerializer ignores collections of interfaces!
XmlSerializer 忽略接口集合!
And by that I mean ignore. While you will get an exception for a line like:
我的意思是ignore。虽然您会收到类似以下行的异常:
public IFoo Foo { get; set; }
you will notget an exception for:
你不会得到以下例外:
public ICollection<IFoo> LotsOfFoos { get { return this.fooBackingField; } }
回答by Shimmy Weitzhandler
You can implement the IXmlSerializer
and do the serialization manually, and benefit from serializing properties, and vice versa, deserializing them using constructors / private field assignment.
您可以IXmlSerializer
手动实现和执行序列化,并从序列化属性中受益,反之亦然,使用构造函数/私有字段分配反序列化它们。