By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

Serialization Question

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Nov 19 '05 #2

P: n/a

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/...9/21/4164.aspx

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

Thanks,

Ethem


"Matt Berther" wrote:
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


Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.