XMLSerializer and custom strong typed Collection
This works great until i have a object that has a custom collection as a property.
This is my collection:
publicclass CarCollection : System.Collections.CollectionBase
{
public Carthis[int index]//This is my indexer
{
{
return (Car)this.List[index];
}
set
{
this.List[index] =value;
}
}publicvoid Add(Car car)
{
this.List.Add(car);
}
//and some other functions, like remove, count, and so on
}
The car class is a simple class that has some public properties.
The last class that i have is a ResultClass. The reason that i have this class is that i have read that u cant serialize a class that inherits from System.Collections.CollectionBase. Therefore i have made a ResultClass that has a CarCollection property and some other props, the ResultClass works as a wrapper.
my serializer:
privatestaticstring ConvertObjectToXML(object sourceObject)
{
XmlSerializer xmls =new XmlSerializer(sourceObject.GetType());
StringBuilder sb =new StringBuilder();
StringWriter sw =new StringWriter(sb);
XmlTextWriter xmltw =new XmlTextWriter(sw);
xmltw.Formatting = Formatting.None;
xmls.Serialize(xmltw, sourceObject);
return sb.ToString();
}
When i look in the XML file that is generated, the resultClass is a attribute element it shoud be a root element with child element/attributes. does anyone know how to serialize a object that contains a collection property? - Best regard YarakMan
I call the serializer like this
ConvertObjectToXML(resultClass)

