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.