I am reading this file and using the System.Xml.Serialization.XmlSerializer to serialize and deserialize. Sorry I am unable to post the contents of the file in this question as StackOverflow is encoding them incorrectly.
The Deserialize function is throwing an exception.
'', hexadecimal value 0x03, is an invalid character. Line 5, position 20.
What am I doing wrong?
Here is my code. Also Available here
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace XMLTester
{
public class HexTester
{
public static void Main(string[] args)
{
var lines = File.ReadAllLines(@"..\..\HexText.txt"); ;
var foo = new Foo()
{
Items = new List<FooBar>()
{
new FooBar()
{
Text = lines[0]
}
}
};
string xml = SerializeToXML(foo);
var objTabs = DeserializeFromXML(xml, typeof(Foo)); //This throws an error
}
static string SerializeToXML(object obj)
{
StringBuilder xml = new StringBuilder();
XmlSerializer serializer = new XmlSerializer(obj.GetType());
TextWriter textWriter = new StringWriter(xml);
serializer.Serialize(textWriter, obj);
textWriter.Close();
return xml.ToString();
}
static object DeserializeFromXML(string xml, Type toType)
{
XmlSerializer deserializer = new XmlSerializer(toType);
TextReader textReader = new StringReader(xml);
Object obj = deserializer.Deserialize(textReader); //This throws an error
textReader.Close();
return obj;
}
}
public class Foo
{
public List<FooBar> Items { get; set; }
}
public class FooBar
{
public string Text { get; set; }
}
}