473,378 Members | 1,518 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,378 software developers and data experts.

Can't XML Serialize my object

Am i using the XmlSerializer incorrectly? Im using C# v1.1. My code:

public class Agenda
{
public int a=1;
public ArrayList val = new ArrayList();
}

public class Section
{
public int b=3;
}

Agenda agenda = new Agenda();
agenda.val.Add(new Section());

XmlSerializer serializer = new XmlSerializer(typeof(Agenda));
StringWriter sw = new StringWriter();
serializer.Serialize(sw, agenda);
sw.Close();

I get the following error within VS.NET 2003:

An unhandled exception of type 'System.InvalidOperationException'
occurred in system.xml.dll
Additional information: There was an error generating the XML document.

The problem seems to be with the ArrayList adding an instance that is
not an Agenda (in this case, Section object). When it is removed all
is fine. Any way around this problem?

Jan 17 '07 #1
1 1687
An ArrayList stores items of type Object, so the serializer is having
trouble specifying what type it should be (unlike in, say, XAML, where you
can map CLR namespaces to XML namespaces), so you have to either replace
ArrayList with Section[] or SectionCollection : CollectionBase or
List<Section(if you're using .Net 2.0) to get a type-specific collection
that the serializer can reflectively determine item type or you can just
tell the serializer that that property takes items of type Section with
[XmlArrayItem(typeof(Section))]

Jan 23 '07 #2

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

Similar topics

7
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
1
by: Michael | last post by:
Hi I anyone have a clue or can solve my problem I would be glad :-) Regards Michael I have a problem with creating an XML-document where the returning data from the webservice, have been...
3
by: Kristian Kjems | last post by:
This is the example from MSDN where an object is serialized to a filestream: MySerializableClass myObject = new MySerializableClass(); // Insert code to set properties and fields of the object....
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
0
by: Ken Durden | last post by:
I'm getting the following exception coming out of a block of unmanaged C++ code. Thread executed for 1.03 sec and died with the following exception: External component has thrown an exception. ...
3
by: Jerry | last post by:
Hi, I have a class like the following: class A { private B _b; A (B b) { _b = b; } ...
1
by: Tim | last post by:
Could anyone tell me what this means and how do I correct it. Any suggestions? Thanks! Tim Richardson IT Developer and Consultant www.paladin3d.com Unable to serialize the session state. In...
10
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
3
by: SteveT | last post by:
I want to be able to deserialize data contained within a XML file to a data structure. This I know how to do. However, what I want to be able to do is the following: 1. Serialize multiple "C#...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?

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.