Using XmlSerializer

  • Thread starter Thread starter jw56578
  • Start date Start date
J

jw56578

What do i need to add to my class so that the ArrayList the holds a
collection of my class will serialize properly.
heres my class

[System.Xml.Serialization.XmlInclude(typeof(Card))]
public struct Card
{
private int numbericvalue;
private CardDeck.Suits suit;
private CardDeck.Types type;


public CardDeck.Suits Suit
{
set
{
suit = value;
}
get
{
return suit;
}

}
public CardDeck.Types Type
{
set
{
type = value;
}
get
{
return type;
}

}


}

I create an ArrayList, add the class to the ArrayList and try to call
XmlSerializer.Serialiaze(myArrayList);

i recieve this error:
The type Card
was not expected.
Use the XmlInclude or SoapInclude
attribute to specify types that are not known statically

what else do i have to add to my Card class so that it will work
 
(e-mail address removed) wrote:

I create an ArrayList, add the class to the ArrayList and try to call
XmlSerializer.Serialiaze(myArrayList);

i recieve this error:
The type Card
was not expected.
Use the XmlInclude or SoapInclude
attribute to specify types that are not known statically

what else do i have to add to my Card class so that it will work

You need to use the XmlInclude attribute on the collection class, not on
the object class. I think you can also use it on a property that has the
collection type, but I'm not completely sure. So, this might work:

class MyClass {
private ArrayList list;
[XmlInclude(typeof(Card))]
public ArrayList List {
get { return list; }
set { list = value; }
}
}

And I'm sure it will work if your derive your own collection type from
CollectionBase (in .NET 1, I think things are different in .NET 2 when
using a List<Card>) and put the attribute on the collection class.

Finally I should say, why didn't you post this to dotnet.xml?


Oliver Sturm
 
Back
Top