469,271 Members | 1,270 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Error deserializing previously serialized object

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;
}
}
}
Nov 12 '05 #1
2 14699
"Richard" <kh******@hotmail.com> wrote in message news:ee**************************@posting.google.c om...
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. : : TextWriter writer = new System.IO.StreamWriter("Properties");
serializer.Serialize(writer,props); : : TextReader reader = new System.IO.StringReader("Properties");
props = (DataGridProperties)serializer.Deserialize(reader) ;


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
Nov 12 '05 #2
Didn't even realize I was using a StringReader... just needed another
pair of eyeballs... Thanks Derek!
"Derek Harmon" <lo*******@msn.com> wrote in message news:<u4**************@tk2msftngp13.phx.gbl>...
"Richard" <kh******@hotmail.com> wrote in message news:ee**************************@posting.google.c om...
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.

: :
TextWriter writer = new System.IO.StreamWriter("Properties");
serializer.Serialize(writer,props);

: :
TextReader reader = new System.IO.StringReader("Properties");
props = (DataGridProperties)serializer.Deserialize(reader) ;


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

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Justin Armstrong | last post: by
4 posts views Thread by ron | last post: by
5 posts views Thread by Lucas | last post: by
7 posts views Thread by farseer | last post: by
5 posts views Thread by Solel Software | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.