473,395 Members | 1,649 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,395 software developers and data experts.

Deserializing of a XML string as a object does not work with .NET 1.1 SP1 anymore

Hi all,

We have problems deseralizing objects previously serialized as XML.
This did work fine with .NET 1.1 but since we have installed SP1,
deserializing fails (but serializing works). The error occurs within
the following line "return formatter.Deserialize(xmlReader) as
ParameterList;" and the "innerexception" sais "The root element is
missing.".

Thanks for any hints

Chrigel

Here is the code:
--------------

using System;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
ParameterList l = new ParameterList();
string s = l.SerializeToString();
l = null;
l = ParameterList.Deserialize(s);
s = "";
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
}

[Serializable]
[XmlType("ParameterList1")]
public class ParameterList
{
public string a = "";
public ParameterList()
{
a = "hugo";
}

public static ParameterList Deserialize(string sVesselData)
{
MemoryStream stream = new MemoryStream();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sVesselData);
stream.Write(bytes,0,bytes.Length);
XmlSerializer formatter = new XmlSerializer(typeof(ParameterList));
System.Xml.XmlTextReader xmlReader = new
System.Xml.XmlTextReader(stream);
stream.Seek(0,0);
return formatter.Deserialize(xmlReader) as ParameterList;
}

public string SerializeToString()
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Xml.Serialization.XmlSerializer formatter = new
XmlSerializer(typeof(ParameterList));
System.Xml.XmlTextWriter xmlWriter = new
System.Xml.XmlTextWriter(stream,System.Text.Encodi ng.UTF8);
xmlWriter.Flush();
stream.Seek(0,0);
formatter.Serialize(xmlWriter, this);
return System.Text.Encoding.UTF8.GetString(stream.GetBuff er());
}
}
}
Nov 12 '05 #1
3 8067
I haven't done any recent serialization/deserialization myself, but one
program I wrote some time ago (in the VS.2002 days) did, and I noticed then
that the XML file created by the serialization process wasn't well-formed
XML. In fact there was no root element, which is exactly what your
deserialization error is reporting. Perhaps SP1 changes the rules in some
way.

Tom Dacon
Dacon Software Consulting

"Chrigel" <ch*****************@hotmail.com> wrote in message
news:c7**************************@posting.google.c om...
Hi all,

We have problems deseralizing objects previously serialized as XML.
This did work fine with .NET 1.1 but since we have installed SP1,
deserializing fails (but serializing works). The error occurs within
the following line "return formatter.Deserialize(xmlReader) as
ParameterList;" and the "innerexception" sais "The root element is
missing.".

Thanks for any hints

Chrigel

Here is the code:
--------------

using System;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
ParameterList l = new ParameterList();
string s = l.SerializeToString();
l = null;
l = ParameterList.Deserialize(s);
s = "";
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
}

[Serializable]
[XmlType("ParameterList1")]
public class ParameterList
{
public string a = "";
public ParameterList()
{
a = "hugo";
}

public static ParameterList Deserialize(string sVesselData)
{
MemoryStream stream = new MemoryStream();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sVesselData);
stream.Write(bytes,0,bytes.Length);
XmlSerializer formatter = new XmlSerializer(typeof(ParameterList));
System.Xml.XmlTextReader xmlReader = new
System.Xml.XmlTextReader(stream);
stream.Seek(0,0);
return formatter.Deserialize(xmlReader) as ParameterList;
}

public string SerializeToString()
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Xml.Serialization.XmlSerializer formatter = new
XmlSerializer(typeof(ParameterList));
System.Xml.XmlTextWriter xmlWriter = new
System.Xml.XmlTextWriter(stream,System.Text.Encodi ng.UTF8);
xmlWriter.Flush();
stream.Seek(0,0);
formatter.Serialize(xmlWriter, this);
return System.Text.Encoding.UTF8.GetString(stream.GetBuff er());
}
}
}

Nov 12 '05 #2
Hi all,

Thanks to Pascal (pag) who sent me the following link:

http://www.dotnettalk.net/showthread.php?t=262

Microsoft made the following change to XmlTextReader:

1) In the constructor of XmlTextReader, they read 4kB from the stream you
pass in.
2) The constructor blocks until it gets a full 4k
3) Every time you call XmlTextReader.Read(), if there isn't enough data in
the internal buffer, it reads a full 4k. And *blocks* until it gets a full
4k.

They wanted to fix another issue but it seems this has created
another problem. The workaround is to read the contents of the stream into
some buffer such as a MemoryStream then pass that to the XmlTextReader.

So I changed the my Deserialize(..) method as follows:

public static ParameterList Deserialize(string sVesselData)
{
MemoryStream stream = new MemoryStream();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sVesselData);
stream.Write(bytes,0,bytes.Length);
XmlSerializer formatter = new XmlSerializer(typeof(ParameterList));
System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(stream);
stream.Seek(0,0);

StreamReader t_reader = new StreamReader(stream);
string mystring = t_reader.ReadToEnd();
xmlReader = new System.Xml.XmlTextReader(new StringReader(mystring));

return formatter.Deserialize(xmlReader) as ParameterList;
}

And this works with or without SP1!

Regards, Chrigel
Nov 12 '05 #3
Yikes!

TD

"Chrigel" <ch*****************@hotmail.com> wrote in message
news:c7*************************@posting.google.co m...
Hi all,

Thanks to Pascal (pag) who sent me the following link:

http://www.dotnettalk.net/showthread.php?t=262

Microsoft made the following change to XmlTextReader:

1) In the constructor of XmlTextReader, they read 4kB from the stream you
pass in.
2) The constructor blocks until it gets a full 4k
3) Every time you call XmlTextReader.Read(), if there isn't enough data in
the internal buffer, it reads a full 4k. And *blocks* until it gets a full
4k.

They wanted to fix another issue but it seems this has created
another problem. The workaround is to read the contents of the stream into
some buffer such as a MemoryStream then pass that to the XmlTextReader.

So I changed the my Deserialize(..) method as follows:

public static ParameterList Deserialize(string sVesselData)
{
MemoryStream stream = new MemoryStream();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sVesselData);
stream.Write(bytes,0,bytes.Length);
XmlSerializer formatter = new XmlSerializer(typeof(ParameterList));
System.Xml.XmlTextReader xmlReader = new
System.Xml.XmlTextReader(stream);
stream.Seek(0,0);

StreamReader t_reader = new StreamReader(stream);
string mystring = t_reader.ReadToEnd();
xmlReader = new System.Xml.XmlTextReader(new StringReader(mystring));

return formatter.Deserialize(xmlReader) as ParameterList;
}

And this works with or without SP1!

Regards, Chrigel

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kenneth Baltrinic | last post by:
I am getting the following error when deserializing an object that has a couple of dozen dependant objects in its object graph. Anyone who can suggest where I might begin to look to resolve problem...
1
by: Thomas | last post by:
Hi, I implemented a composite pattern which should be serializable to xml. After spending some time in the newsgroups, i finally managed serializing, even with utf-8 instead of utf-16, which...
3
by: Mark McConnell | last post by:
Regarding deserializing XML into a custom object... I've been able to deserialize an XML doc into my custom object and everything works great. The problem I am encountering is when one of the...
4
by: Wayne Wengert | last post by:
Using VB.NET I want to read in an XML file that has an array of objects and then step through the resulting array in code. I build a class to define the structure and I am running code to read in...
2
by: Phillip Galey | last post by:
I have an object called Place which contains only string properties and has the <Serializable()> flag before the class name declaration. I also have a collection object called Places, which is...
2
by: Drolem | last post by:
Hello All, I use the following method to serialize a settings class to a string: '======================================================================= Public Shared Function...
9
by: PeterWellington | last post by:
I have a column in a data table that stores enum values and assigns a default value: Dim dc As New DataColumn("TestEnumField", GetType(DayOfWeek)) dc.DefaultValue = DayOfWeek.Thursday When I...
1
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an...
0
by: =?Utf-8?B?dm96ZWxkcg==?= | last post by:
I'm having an issue in deserializing an object from XML. I generated my XML schema from my .NET dll using xsd.exe. I've used that schema in another application. That application spits out XML based...
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:
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.