473,387 Members | 1,698 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.

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 14946
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Armstrong | last post by:
I'm having difficulties deserializing some objects. Consider the following example of what I'm trying to do: ---------------------------------------------------------- class Person { string...
0
by: Ran Davidovitz | last post by:
Hi all. In my company we have a webservice that exposes an OBJECT, the company that made the webservice gave us the object dll so that we can have there functionallity (lots of logic). As a...
4
by: Daniel Reber | last post by:
I have an exe that serializes one of it's classes to disk. I also have the exact same .cs file that contains the class that was serialized, in a Windows service application. I am trying to load...
4
by: ron | last post by:
Hi, I have class object that i serialize using the System.Xml.Serialization class. Intermittently the object is not getting serialized correctly, using System.Xml.Serialization classes....
5
by: Lucas | last post by:
Well, here's the situation.. It's pretty simple; just that I can't get it to work. I have 2 Executables.. The first one is called CSharp.exe which is a simple WinForm App. I have a single...
7
by: farseer | last post by:
Hi, i am getting an error when trying to deserialize and xml. the error is shown below. i get this error regardless if i try to desearialize in C# or VB. the XML itself does infact contain a...
5
by: Solel Software | last post by:
Hello, I am attempting to serialize an object for storage in a DB field and them deserialize it later on. I have managed to Serialize it using private string SerializeObject(object...
2
by: ce | last post by:
Being a newbie regarding serialization and memorystreams, I was trying to see if i could improve page performance (avoiding going to the db on a postback) by saving my serialized business object in...
5
by: Veilchen | last post by:
Hi, I need to deserialize an existing object (not generate its code) from a serialized XML file. The only information I have about how the deserialization method is an input XML schema (which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.