Saturday, March 24, 2012

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

0 comments:

Post a Comment