Groups | Blog | Home
all groups > dotnet xml > september 2004 >

dotnet xml : Error deserializing previously serialized object


khatovar NO[at]SPAM hotmail.com
9/9/2004 3:20:03 PM
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(DataGridProperties));
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(DataGridProperties));
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;
}
}


Derek Harmon
9/9/2004 10:40:57 PM
[quoted text, click to view]

When you save the serialized XML (in UTF-8 encoding) you use
StreamWriter. Why do you use StringReader (it only supports
UTF-16 encoding) to load that XML?

The error is coming from the encoding attribute being incorrect.
StringReader can tell right off the bat that the instance document
isn't really UTF-16 encoded.

Try using a StreamReader instead of StringReader (or alternately,
use a StringWriter to produce the XML which will put a UTF-16
encoding pseudo-attribute in the XML declaration).


Derek Harmon

khatovar NO[at]SPAM hotmail.com
9/10/2004 8:09:14 AM
Didn't even realize I was using a StringReader... just needed another
pair of eyeballs... Thanks Derek!
[quoted text, click to view]
AddThis Social Bookmark Button