Serializing a List exported as an ICollection to XML?

The XmlSerializer requires that serializable properties have a setter. Besides that, the XmlSerializer can not serialize interface properties. The following code will work.

The XmlSerializer requires that serializable properties have a setter. Besides that, the XmlSerializer can not serialize interface properties. The following code will work: XmlElement("Bar") public List Bars { get { return bar_; } set { throw new NotSupportedException("This property 'Bars' cannot be set.

This property is readonly. "); } } If you don't like this solution (the exception is kinda ugly) then you could implement IXmlSerializable and write your own custom serialization. Edit: Artur Mustafin is right, members that implement IEnumerable or ICollection don't need a setter, as is explained on this msdn page: The XmlSerializer gives special treatment to classes that implement IEnumerable or ICollection.

A class that implements IEnumerable must implement a public Add method that takes a single parameter. The Add method's parameter must be of the same type as is returned from the Current property on the value returned from GetEnumerator, or one of that type's bases. A class that implements ICollection (such as CollectionBase) in addition to IEnumerable must have a public Item indexed property (indexer in C#) that takes an integer, and it must have a public Count property of type integer.

The parameter to the Add method must be the same type as is returned from the Item property, or one of that type's bases. For classes that implement ICollection, values to be serialized are retrieved from the indexed Item property, not by calling GetEnumerator.

1 This is wrong and ugly answer, this is wrong because it works event without setter, see my post – Artur Mustafin Sep 30 at 1:18 The XmlSerializer DO NOT requires that serializable properties have a setter – Artur Mustafin Sep 30 at 2:50.

Giving a correct answer, there is no point to create ugly setter to the List public property, to throw an exception. This is because List is already implements ICollection and provides method with signature void Add(T object) which is used by Serialization mechanism; You are only have to add the setter to the public properties being serialized, and change ICollection to List: XmlRoot("Foo") public class Foo { private List bar_ = new List(); XmlElement("Something") public string Something { get; set; } XmlElement("Bar") public List Bars { get { return bar_; } } } You will get an output: My String Also, it is a better idea to serialize xml in-meemory, to see the results, or test it, as follows: static void Main(string args) { Bar b1 = new Bar(); // populate b1 with interesting data Bar b2 = new Bar(); // populate b2 with interesting data Foo f = new Foo(); f.Bars. Add(b1); f.Bars.

Add(b2); f. Something = "My String"; using (MemoryStream ms = new MemoryStream()) using (System.IO. TextWriter textWriter = new System.IO.

StreamWriter(ms)) { System.Xml.Serialization. XmlSerializer serializer = new System.Xml.Serialization. XmlSerializer(typeof(Foo)); serializer.

Serialize(textWriter, f); string text = Encoding. UTF8. GetString(ms.ToArray()); Console.

WriteLine(text); } Console. ReadKey(false); } To serialize using interfaces, use my project XmlSerialization.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions