Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / September 2005

Tip: Looking for answers? Try searching our database.

XmlSerializer with array of classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
zatopek - 13 Sep 2005 06:37 GMT
I have a class that i wqant to serialize. This class contains an arraylist.
When i fill this with strings, it files just as i want. But when i populate
it with my custom class, i crashes.

What do i do wrong?
My hopes was to get the xml-output from my model "for free".
But maybee there's nothing as a free lunch.
The code-snippets are c++.

Here's the filing code
-----
Class1* c1 = new Class1();
   
FileStream* xmlStream = new FileStream("model.xml", FileMode::Create,
FileAccess::Write);
XmlSerializer* serializer = new XmlSerializer( __typeof(Class1) );
serializer->Serialize(xmlStream, c1);

This is class 1
-----
public:
Class1(void);
~Class1(void);

[XmlArray("DataMembers") ]
ArrayList*    m_arList;

If i do this in the constructor, it works
-------
Class1::Class1(void)
{
m_arList = new ArrayList();
m_arList->Add( new String("1") );

But this crashes
------
Class1::Class1(void)
{
m_arList = new ArrayList();
m_arList->Add( new Class2() );

And this is Class2
----
public:
Class2(void);
~Class2(void);
[XmlElement("Int") ]
int m_myInt;
dennis.doomen@gmx.net - 13 Sep 2005 11:24 GMT
In order for the XmlSerializer to understand how it it supposed to
serialize elements of an Array or other collection, you must tell it
what kind of objects the array may contain. One way to do that is to
use the [XmlArray] and [XmlArrayItem] attributes and specify the
supported data types.

Hth,

Dennis Doomen
www.dennisdoomen.net
zatopek - 13 Sep 2005 11:50 GMT
I did tag the member variabel containg the ArrayList as XmlArray.
Do you men that i should tag class2 as XmlArrayItem.

If so, how will that limit my options to use Class2 not in an array?

/M

> In order for the XmlSerializer to understand how it it supposed to
> serialize elements of an Array or other collection, you must tell it
[quoted text clipped - 6 lines]
> Dennis Doomen
> www.dennisdoomen.net
zatopek - 13 Sep 2005 12:51 GMT
Solved it.
Found example om "XmlArrayItemAttribute Constructor (Type)"

I Put the XmlArrayItem on the wrong level.
Should've been on tha class containing the items. I.e. Class1

[XmlArrayItem("DataMembers",  __typeof(Class2)) ]
ArrayList*    m_arList;

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.