I have created two functions, XML serialize and deserialize. The problem is that I get an error on derserialization. Below you may find the functions:
public string Serialize(Object o)
{
using (var writer = new StringWriter())
{
new XmlSerializer(o.GetType()).Serialize(writer, o);
return writer.ToString();
}
}
public PathDetailsMessage Deserialize(string xml)
{
using (TextReader reader = new StringReader(xml))
{
XmlSerializer serializer = new XmlSerializer(typeof(PathDetailsMessage));
return (PathDetailsMessage)serializer.Deserialize(reader);
}
}
And the calls:
static void Main(string[] args)
{
PathDetailsBLL train = new PathDetailsBLL();
PathDetailsMessage pdm = train.GetDetails();
string xml = train.Serialize(pdm);
PathDetailsBLL dsa = new PathDetailsBLL();
PathDetailsMessage fds = new PathDetailsMessage();
fds = dsa.Deserialize(pdm.ToString());
Console.Write(fds);
Console.ReadKey();
}
On the line return (PathDetailsMessage)serializer.Deserialize(reader);
I get the following error:
System.InvalidOperationException: 'There is an error in XML document (1, 1).'
XmlException: Data at the root level is invalid. Line 1, position 1.
Can you help me?
Thank you.