Serialization Question

G

Guest

Hi,

I have a collection class called Printers that implement the ICollection and
IEnumerable. This class contains a private ArrayList that holds the Printer
objects. The printer class is a serializable class that contains only the
public name attribute.

After serialization, I want to get the following xml structure;

<Printers>
<Printer>
<Name></Name>
</Printer>
<Printers>

When I want to serialize this Printers collection, I get instead something
like;

<ArrayOfPrinter>
<Printer>
<Name></Name>
</Printer>
</ArrayOfPrinter>

If I take out the ICollection and IEnumerable away, (turning the class into
a normal one) and make the ArrayList public, then I get the following;

<Printers>
<NameOfMyArrayList>
<Printer>
<Name></Name>
</Printer>
</NameOfMyArrayList>
</Printers>

For clarity, I don't include any code at the moment.

Any help appreciated,

Ethem
 
M

Matt Berther

Hello Ethem,

I'm assuming you mean XmlSerialization... Given that, either the XmlRoot
(if its the top level node) or XmlElement attributes will work.

[XmlRoot("Printers")]
public class PrinterCollection
{
// rest of your code
}
 
G

Guest

Hi Matt,

Thanks for the response. Unfortunately, my collection class implements
ICollection and IEnumerable. If the collection implements these interfaces,
you cannot define an XmlRoot attribute (or any other attribute) on the class
level. Same applies if you derive the collection class from the
CollectionBase.

For my problem, I found a workaround in the following site:

http://dotnet.org.za/armand/archive/2004/09/21/4164.aspx

It seems that you can make it programmatically instead of decleratively.

Thanks,

Ethem




Matt Berther said:
Hello Ethem,

I'm assuming you mean XmlSerialization... Given that, either the XmlRoot
(if its the top level node) or XmlElement attributes will work.

[XmlRoot("Printers")]
public class PrinterCollection
{
// rest of your code
}

--
Matt Berther
http://www.mattberther.com
Hi,

I have a collection class called Printers that implement the
ICollection and IEnumerable. This class contains a private ArrayList
that holds the Printer objects. The printer class is a serializable
class that contains only the public name attribute.

After serialization, I want to get the following xml structure;

<Printers>
<Printer>
<Name></Name>
</Printer>
<Printers>
When I want to serialize this Printers collection, I get instead
something like;

<ArrayOfPrinter>
<Printer>
<Name></Name>
</Printer>
</ArrayOfPrinter>
If I take out the ICollection and IEnumerable away, (turning the class
into a normal one) and make the ArrayList public, then I get the
following;

<Printers>
<NameOfMyArrayList>
<Printer>
<Name></Name>
</Printer>
</NameOfMyArrayList>
</Printers>
For clarity, I don't include any code at the moment.

Any help appreciated,

Ethem
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top