XML serialisation

  • Thread starter Thread starter M
  • Start date Start date
M

M

Hi,

I have a collection of user defined types called
EmployeeCollection which inherits from CollectionBase.
EmployeeCollection is a collection of Employee objects.
When i serialise the collection, the serialized collection
looks like:
<ArrayOfEmployee>
<Employee>
.....
</Employee>
</ArrayOfEmployee>

I tried specifying the XMLRootAttribute for the collection
class, but that gives an error reflecting
EmployeeCollection.
Can anyone please let me know how I can specify my own
element name for the serialised output.

Thanks.
M
 
Here is what you do for your own tag name for the serialized output.
You could specify your own element name by using "XmlElement" attribute
of type "XmlElementAttribute".

ex)
class A {
private int abc;
[XmlElement("Garbage")]
public in Abc {
// get and set.
}
}

After serializing the class "A",
You will get something like
<A xmlns:xsd=htt.....>
<Garbage>
(Some Garbage integer value)
</Garbage>
</A>

So instead of "Abc", you get the tag named "Garbage" using XmlElement
attribute.

Hope that helped.
 
Back
Top