473,395 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

XmlSerializer with array of classes

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;
Sep 13 '05 #1
3 2421
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

Sep 13 '05 #2
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

"de***********@gmx.net" wrote:
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

Sep 13 '05 #3
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;

Sep 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Zion Zadik | last post by:
Dear all, I have a set of c# data classes which i need to fill their data from xml files. serialization looks to be the best way to accomplish this task. Since the data classes are compiled and...
8
by: Harris Boyce | last post by:
Hello, I'm trying to use the FOR XML EXPLICIT clause with SQL Server to deserialize data from my database into a strongly-typed collection object that I will use throughout my application. I...
2
by: Chris Aitchison | last post by:
Hello, I am attempting to have a class that I have written serialize so that it can be both passed as a parameter or return value for a webservice, and also be serialized to disk using the...
16
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
3
by: Bob Rundle | last post by:
I'm trying to serialize a class with XmlSerializer. This class implements the IEnumerable interface. I implemented the IEnumerable interface for reasons other than Xml serialization. However I...
2
by: Kent Boogaart | last post by:
Hello all, I have two simple classes: Item and ItemCollection. Item stores a label for the item and an instance of ItemCollection for all child items. ItemCollection just stores a collection of...
3
by: Don McNamara | last post by:
Hi, I've hit quite a strange problem with XmlSerializer on my W2K3 server. When I serialize/deserialize using an exe on my local computer (XP), everything works fine. When I put the code out on...
1
by: Lloyd Dupont | last post by:
I get an XmlDocument from an external source. I want to load it/map it to C# class. I'm creating classes as I learn about the format. many tag are yet unparsed, but that seems ok for the...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.