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());
}
}
}