473,326 Members | 2,173 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,326 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 2419
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.