Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my public property serialized by the XmlSerializer?

People also ask

How do you know if an object is serialized?

You can determine whether an object is serializable at run time by retrieving the value of the IsSerializable property of a Type object that represents that object's type.

How do you ignore property serializable?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

What is DataContractSerializer and how its different from XmlSerializer?

DataContractSerializer can able to serialize types that implements Idictionary whereas XML serializer not. DataContractSerializer serializes all members which are marked with [DataMember] attribute even if member is marked private. XML serializer serialize only public members.

How does the XmlSerializer work C#?

The XmlSerializer creates C# (. cs) files and compiles them into . dll files in the directory named by the TEMP environment variable; serialization occurs with those DLLs. These serialization assemblies can be generated in advance and signed by using the SGen.exe tool.


As mentioned, most properties must have both a getter and setter; the main exception to this is lists - for example:

private readonly List<Foo> bar = new List<Foo>();
public List<Foo> Bar {get { return bar; } } // works fine

which will work fine; however, if XmlSerializer finds a setter - it demands that it is public; the following will not work:

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]

Any of these will cause it not to serialize


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!

Intro-to-XML Serialization http://www.freeimagehosting.net/uploads/2f04fea2db.png

(having a little too much fun with Freeimagehosting.net)


Also properties that return null are not serialized!


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...

i.E.

public string ID { get { return _item.ID.ToString(); } set { } }

One more thing to add about serialization of collections:

The XmlSerializer ignores collections of interfaces!

And by that I mean ignore. While you will get an exception for a line like:

public IFoo Foo { get; set; }

you will not get an exception for:

public ICollection<IFoo> LotsOfFoos { get { return this.fooBackingField; } }