I've used XMLSerializer before to store user settings with great
success, but this is the first time I've tried to serialize an Array.
It serializes fine, but when I try to deserialize it, I get the
following error:
There is an error in XML document (0, 0). --->
System.Xml.XmlException: The data at the root level is invalid. Line
1, position 1.
As I've done this before, I'm at a loss to explain it.
Here is the xml produced by the serialization routine:
<?xml version="1.0" encoding="utf-8"?>
<DataGridProperties xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ColumnWidths>
<anyType xsi:type="xsd:int">75</anyType>
<anyType xsi:type="xsd:int">75</anyType>
<anyType xsi:type="xsd:int">75</anyType>
<anyType xsi:type="xsd:int">75</anyType>
<anyType xsi:type="xsd:int">75</anyType>
<anyType xsi:type="xsd:int">75</anyType>
<anyType xsi:type="xsd:int">75</anyType>
</ColumnWidths>
</DataGridProperties>
Here is the code:
All I do is create an object of type DataGridProperties and call the
add procedure to add columnwidths (I want to add more properties
later, but I wanted to test first).
using System;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using System.Diagnostics;
namespace DataGridEx
{
public class DataGridProperties
{
private ArrayList _columnWidths = new ArrayList();
public DataGridProperties()
{
}
public void Add(int ColumnWidth)
{
ColumnWidths.Add(ColumnWidth);
}
public void Save()
{
DataGridPropertiesSerializer.Save(this);
}
public bool Load()
{
return DataGridPropertiesSerializer.Load(this);
}
public ArrayList ColumnWidths{get{return _columnWidths
;}set{_columnWidths = value;}}
}
public class DataGridPropertiesSerializer
{
public static void Save(DataGridProperties props)
{
XmlSerializer serializer = new
System.Xml.Serialization.XmlSerializer(typeof(Data GridProperties));
try
{
TextWriter writer = new System.IO.StreamWriter("Properties");
serializer.Serialize(writer,props);
writer.Close();
}
catch
{
}
}
public static bool Load(DataGridProperties props)
{
bool ret = true;
XmlSerializer serializer = new
System.Xml.Serialization.XmlSerializer(typeof(Data GridProperties));
try
{
TextReader reader = new System.IO.StringReader("Properties");
props = (DataGridProperties)serializer.Deserialize(reader) ;
reader.Close();
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
ret = false;
}
return ret;
}
}
}