Showing posts with label arraylist. Show all posts
Showing posts with label arraylist. Show all posts

Saturday, March 24, 2012

Serialization Question

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,
EthemHello 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
>
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...09/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
>
>
>

Serialization Question

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,

EthemHello 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
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...09/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
>

serialize class to the viewstate

I am trying to serialize a class called Order to the ViewState. Order has
one property called LineItems, which is an ArrayList. The ArrayList holds
instances of a class named LineItem. Bother Order and LineItem are marked
with the <Serializable> attribute.

When I attempt to serialize the class using this code:

Dim xs As XmlSerializer = New XmlSerializer(GetType(Order))

Dim sw As New StringWriter()

xs.Serialize(sw, _Order)

ViewState("LineItems") = sw.ToString()

On the line xs.Serialize(sw, _Order), I get an error that reads:

The type LineItem was not expected. Use the XmlInclude or SoapInclude
attribute to specify types that are not known statically.

What am I missing?

Thanks,

Craig BuchananYou may have to specify what the array list holds with the XmlElement
attribute.

I have a list that holds two different types and I had to do apply the
following to the array list:
<XmlElement(Type:=GetType(StringCriteriaField)),
XmlElement(Type:=GetType(IntegerCriteriaField))
"Craig Buchanan" <someone@.somewhere.com> wrote in message
news:#X3bk8R5DHA.504@.TK2MSFTNGP11.phx.gbl...
> I am trying to serialize a class called Order to the ViewState. Order has
> one property called LineItems, which is an ArrayList. The ArrayList holds
> instances of a class named LineItem. Bother Order and LineItem are marked
> with the <Serializable> attribute.
> When I attempt to serialize the class using this code:
> Dim xs As XmlSerializer = New XmlSerializer(GetType(Order))
> Dim sw As New StringWriter()
> xs.Serialize(sw, _Order)
> ViewState("LineItems") = sw.ToString()
> On the line xs.Serialize(sw, _Order), I get an error that reads:
> The type LineItem was not expected. Use the XmlInclude or SoapInclude
> attribute to specify types that are not known statically.
> What am I missing?
> Thanks,
> Craig Buchanan