XMLSerializer and custom strong typed Collection

Im using XMLSerializer to convert my object to xml string.
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
{
get
{
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();
}


I call the serializer like this
ConvertObjectToXML(resultClass)

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

[3136 byte] By [YarakMan] at [2008-2-27]
# 1
I notice that CarCollection is not marked as [Serializable] nor does it implement ISerializable. The way I usually do it is:
Your Object is marked with both and implements the proper interfaces.
Then your Strongly Typed Collection also is marked as such.
MarcD at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 2
I have tried marking all my classes as [Serializable], but this does not make any changes. And i dont use the ISerializable interface, because it is not required when using XMLSerializer (System.Xml.Serialization)

- YarakMan

YarakMan at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 3
Adding and implementing ISErializable speeds up the serialization/deserialiation process because when you implement the constructor overload and the Getojbect interface method then it does not have to use reflection to reflect all of the member types.
What exactly is the problem with the serialization. Are the values being serialized, just not in the format you wish it to be?
MarcD at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 4

Alle the values that are not of the CarCollection type are serialized. In the XML string the carcollection looks like this <carColelction \>.
It is emtry is should have some child elements of the type "car"
it want it to look something like this:
<carCollection>
<car>
<model>Ford</model>
<color>red</color>
</car>
....
</carCollection>

YarakMan at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 5

There is not enough information to answer your question.
Please post the ResultClass definition, the xml instance you get when you serialize the class, and instance you expect/want to get.
You also could generate XSD schema for the ResultClass to see if it matches your expectations. To do so run xsd.exe tool on the assembly containing the ResultClass and specify the full type name:
xsd.exe myassembly.dll /type:<namespace>.ResultClass

Thanks,
Elena
ElenaKharitidi at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...

.NET Development

Site Classified