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

Serializing an ArrayList of Objects

I have an array list of objects that I want to serialize into an XML file. I have tried this using the following code, but it gives me an error message

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

I'm not sure if this is the way that I should go about serializing an array of objects like this, but I would like to get them all into one place kind of like
<waypoints><waypoint><Latitude> 12.2837</Latitude
...
</waypoint><waypoint><Latitude> ..
...
</waypoint></waypoints

Here's the code I was using
using System
using System.Collections
using System.IO
using System.Xml
using System.Xml.Serialization
using System.Text

public class Waypoin

protected double latitude
protected double longitude
protected double elevation
protected int number
protected String name

public double Latitud

get{return latitude;
set{latitude = value;

public double Longitud

get{return longitude;
set{longitude = value;

public double Elevatio

get{return elevation;
set{elevation = value;

public int Numbe

get{return number;
set{number = value;

public String Nam

get{return name;
set{name = value;

public class Ru

public static void Main(

SerializeMyObject()
DeserializeMyObject()
public static void SerializeMyObject(

ArrayList waypoints = new ArrayList()

Waypoint newPoint = new Waypoint()
//Entering Dat
newPoint.Latitude = 56.238716
newPoint.Longitude = 21.3487416
newPoint.Elevation = 5280.0
newPoint.Number = 5
newPoint.Name = "First"

waypoints.Add(newPoint)

newPoint.Latitude = 12.123115
newPoint.Longitude = 86.73846
newPoint.Elevation = 1600.0
newPoint.Number = 10
newPoint.Name = "Second"

waypoints.Add(newPoint)

// The Important code here
XmlSerializer serializer = new XmlSerializer(typeof(ArrayList))
Stream fs = new FileStream("test.xml", FileMode.Create)
XmlWriter writer = new XmlTextWriter(fs, Encoding.Unicode)

serializer.Serialize(writer, waypoints)
// This is where it breaks
fs.Close()
public static void DeserializeMyObject(

ArrayList waypoints = new ArrayList()

Waypoint newPoint = new Waypoint()

XmlSerializer serializer = new XmlSerializer(typeof(ArrayList))
Stream fs = new FileStream("test.xml", FileMode.Open)
XmlReader reader = new XmlTextReader(fs)

waypoints = (ArrayList)serializer.Deserialize(reader)

foreach(Waypoint waypoint in waypoints

System.Console.WriteLine("{0}",waypoint.Name)
fs.Close()


Like I said, I'm not sure if this is the correct way to go about this, but I'd be open to suggestions.
Nov 12 '05 #1
0 2793

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

Similar topics

1
by: Pure Krome | last post by:
How do i serialize two instances (of some object) in the same class? I'm not sure how i would de-serialize a class that has two or more instances of some object type, and how the deserialize...
1
by: Christopher Pragash | last post by:
Hello all, I'm trying to persist an object of type Arraylist into XML using the following the XMLSerializer and it returns an "Unknown" Error. I'm able to serialize and deserialize the arraylist...
3
by: Daniel Lidström | last post by:
Hello, I want to have a class that contains only a collection of another class. For example: public __gc class Alignment { public: Alignment(); ... };
0
by: Ante Smolcic | last post by:
Hi all, I have an ArrayList that contains items of type A. I declared the XmlArrayItem atribute for that type. Now I have an derived type B (from A) also contained in the ArrayList but I get...
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
1
by: chaitanyag | last post by:
Hi, I have my data stored in a set of classes (or structs, doesn't matter), which I am trying to serialize. These classes are stored in an ArrayList, which serializes ok when all the objects in...
13
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data...
5
by: Craig Buchanan | last post by:
I have a class called LineItems. It implements IEnumerable. It uses an ArrayList to hold objects. When I try to serialize the object that contains LineItems (an object called Order), I get an...
0
by: Yofnik | last post by:
Is there a way to avoid serializing an empty ArrayList? I would like the following class: public class Collection { public ArrayList Items = new ArrayList(); } ....to serialize like this:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.