Skip to content

Instantly share code, notes, and snippets.

@codingdawg
Created May 9, 2017 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingdawg/b29091ab48958e57d4e4e1bd053a5cc7 to your computer and use it in GitHub Desktop.
Save codingdawg/b29091ab48958e57d4e4e1bd053a5cc7 to your computer and use it in GitHub Desktop.
XML Deserializer throwing Exception
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));
}
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);
textReader.Close();
return obj;
}
}
public class Foo
{
public List<FooBar> Items { get; set; }
}
public class FooBar
{
public string Text { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment