473,729 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Deser ialize(xmlReade r) 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.Seri alization;

namespace ConsoleApplicat ion1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
ParameterList l = new ParameterList() ;
string s = l.SerializeToSt ring();
l = null;
l = ParameterList.D eserialize(s);
s = "";
}
catch(Exception e)
{
MessageBox.Show (e.Message);
}
}
}

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

public static ParameterList Deserialize(str ing sVesselData)
{
MemoryStream stream = new MemoryStream();
byte[] bytes = System.Text.Enc oding.UTF8.GetB ytes(sVesselDat a);
stream.Write(by tes,0,bytes.Len gth);
XmlSerializer formatter = new XmlSerializer(t ypeof(Parameter List));
System.Xml.XmlT extReader xmlReader = new
System.Xml.XmlT extReader(strea m);
stream.Seek(0,0 );
return formatter.Deser ialize(xmlReade r) as ParameterList;
}

public string SerializeToStri ng()
{
System.IO.Memor yStream stream = new System.IO.Memor yStream();
System.Xml.Seri alization.XmlSe rializer formatter = new
XmlSerializer(t ypeof(Parameter List));
System.Xml.XmlT extWriter xmlWriter = new
System.Xml.XmlT extWriter(strea m,System.Text.E ncoding.UTF8);
xmlWriter.Flush ();
stream.Seek(0,0 );
formatter.Seria lize(xmlWriter, this);
return System.Text.Enc oding.UTF8.GetS tring(stream.Ge tBuffer());
}
}
}
Nov 12 '05 #1
3 8085
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.c om> wrote in message
news:c7******** *************** ***@posting.goo gle.com...
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.Deser ialize(xmlReade r) 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.Seri alization;

namespace ConsoleApplicat ion1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
ParameterList l = new ParameterList() ;
string s = l.SerializeToSt ring();
l = null;
l = ParameterList.D eserialize(s);
s = "";
}
catch(Exception e)
{
MessageBox.Show (e.Message);
}
}
}

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

public static ParameterList Deserialize(str ing sVesselData)
{
MemoryStream stream = new MemoryStream();
byte[] bytes = System.Text.Enc oding.UTF8.GetB ytes(sVesselDat a);
stream.Write(by tes,0,bytes.Len gth);
XmlSerializer formatter = new XmlSerializer(t ypeof(Parameter List));
System.Xml.XmlT extReader xmlReader = new
System.Xml.XmlT extReader(strea m);
stream.Seek(0,0 );
return formatter.Deser ialize(xmlReade r) as ParameterList;
}

public string SerializeToStri ng()
{
System.IO.Memor yStream stream = new System.IO.Memor yStream();
System.Xml.Seri alization.XmlSe rializer formatter = new
XmlSerializer(t ypeof(Parameter List));
System.Xml.XmlT extWriter xmlWriter = new
System.Xml.XmlT extWriter(strea m,System.Text.E ncoding.UTF8);
xmlWriter.Flush ();
stream.Seek(0,0 );
formatter.Seria lize(xmlWriter, this);
return System.Text.Enc oding.UTF8.GetS tring(stream.Ge tBuffer());
}
}
}

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.R ead(), 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(str ing sVesselData)
{
MemoryStream stream = new MemoryStream();
byte[] bytes = System.Text.Enc oding.UTF8.GetB ytes(sVesselDat a);
stream.Write(by tes,0,bytes.Len gth);
XmlSerializer formatter = new XmlSerializer(t ypeof(Parameter List));
System.Xml.XmlT extReader xmlReader = new System.Xml.XmlT extReader(strea m);
stream.Seek(0,0 );

StreamReader t_reader = new StreamReader(st ream);
string mystring = t_reader.ReadTo End();
xmlReader = new System.Xml.XmlT extReader(new StringReader(my string));

return formatter.Deser ialize(xmlReade r) as ParameterList;
}

And this works with or without SP1!

Regards, Chrigel
Nov 12 '05 #3
Yikes!

TD

"Chrigel" <ch************ *****@hotmail.c om> wrote in message
news:c7******** *************** **@posting.goog le.com...
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.R ead(), 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(str ing sVesselData)
{
MemoryStream stream = new MemoryStream();
byte[] bytes = System.Text.Enc oding.UTF8.GetB ytes(sVesselDat a);
stream.Write(by tes,0,bytes.Len gth);
XmlSerializer formatter = new XmlSerializer(t ypeof(Parameter List));
System.Xml.XmlT extReader xmlReader = new
System.Xml.XmlT extReader(strea m);
stream.Seek(0,0 );

StreamReader t_reader = new StreamReader(st ream);
string mystring = t_reader.ReadTo End();
xmlReader = new System.Xml.XmlT extReader(new StringReader(my string));

return formatter.Deser ialize(xmlReade r) 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
2228
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 I would greatly in debted to. Serializing the object works fine. When I try to deserialize it, I get the following error: A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll Additional...
1
1856
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 causes ie problems. But when deserializing the xml into the object structure, the following exception is beeing thrown: There is an error in XML document (3, 701).
3
5564
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 XML elements contains a NULL and it maps to a variable of type Integer in my custom object, the deserializer doesn't like this. For example, my custom object is declared like: Public Class Member
4
7512
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 the data but I can't figure out where the data is in the resulting array. Most of the relevant code is below. When I run the code to desrialize I get no errors but if I try to look at some of the data via the command window I get errors such as...
2
3772
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 implemented using Inherits System.Collections.Specialized.NameObjectCollectionBase and also has the <Serializable()> flag before the class name declaration. In the calling code, I'm successfully serializing the object to an XML file using a...
2
2450
by: Drolem | last post by:
Hello All, I use the following method to serialize a settings class to a string: '======================================================================= Public Shared Function DeflateClass(ByVal serializing As Object, ByVal typeSerializing As System.Type) As String Dim writer As StringWriter = New StringWriter Dim serializer As XmlSerializer = _
9
2948
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 try to serialize/deserialize dataset schema, I get the error below during deserialization: "System.ArgumentException: The DefaultValue for column TestEnumField is of
1
11808
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 XmlNodeList. I am trying to deserialize the nodes using a foreach as follows: foreach(XmlNode lineItem in LineItemsNodeList) An abbreviated example of the nested lineItem node looks like this:
0
2444
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 on user input and workflow. It is sending out the integer value for enums instead of the string values. This seems like it should be the most common sense way to do it (since it's an easy cast from int to an enum (MyEnum)myInt) but XML...
0
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.