Public fields / properties of a class derived from BindingList <T> will not be serialized

I am trying to serialize a class that comes from BindingList (Floor) , where Floor is a simple class that contains only the Floor.Height property

Here's a simplified version of my class

[Serializable] [XmlRoot(ElementName = "CustomBindingList")] public class CustomBindingList:BindingList<Floor> { [XmlAttribute("publicField")] public string publicField; private string privateField; [XmlAttribute("PublicProperty")] public string PublicProperty { get { return privateField; } set { privateField = value; } } } 

I will serialize an instance of CustomBindingList using the following code.

 XmlSerializer ser = new XmlSerializer(typeof(CustomBindingList)); StringWriter sw = new StringWriter(); CustomBindingList cLIst = new CustomBindingList(); Floor fl; fl = new Floor(); fl.Height = 10; cLIst.Add(fl); fl = new Floor(); fl.Height = 10; cLIst.Add(fl); fl = new Floor(); fl.Height = 10; cLIst.Add(fl); ser.Serialize(sw, cLIst); string testString = sw.ToString(); 

However, testString ends with the installation of the following XML:

 <CustomBindingList xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <Floor Height="10" /> <Floor Height="10" /> <Floor Height="10" /> </CustomBindingList>" 

How do I get "publicField" or "publicProperty" for serialization?

+2
source share
3 answers

XML serialization processes collections in a specific way and never serializes the fields or properties of a collection, but only the elements.

You can:

  • implement IXmlSerializable to generate and parse XML yourself (but this is a lot of work)
  • wrap your BindingList in another class in which you declare your custom fields (as suggested by speps).
+2
source

The short answer here is that .NET usually expects be xor collections to have properties. This appears in several places:

  • XML serialization collection properties are not serialized
  • data bindings you cannot bind data to properties in collections, since they implicitly transfer you to the first element

In the case of xml serialization, this makes sense if you think it could just be SomeType[] on the client ... where will the additional data be added?

A common solution is to encapsulate the collection, not

 public class MyType : List<MyItemType> // or BindingList<...> { public string Name {get;set;} } public class MyType { public string Name {get;set;} public List<MyItemType> Items {get;set;} // or BindingList<...> } 

Normally I would not have set for the collection property, but the XmlSerializer requires it ...

+3
source

This is a known issue with XML serialization and collection inheritance.

You can read more about this here: http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/0d94c4f8-767a-4d0f-8c95-f4797cd0ab8e

You can try something like this:

 [Serializable] [XmlRoot] public class CustomBindingList { [XmlAttribute] public string publicField; private string privateField; [XmlAttribute] public string PublicProperty { get { return privateField; } set { privateField = value; } } [XmlElement] public BindingList<Floor> Floors = new BindingList<Floor>(); } 

This means that you can add floors using Floors.Add, and you will get the desired result, I hope, however, I have not tried it. Keep in mind that playing with attributes is the key to serializing XML.

+1
source

All Articles