Created
May 9, 2017 23:15
-
-
Save codingdawg/b29091ab48958e57d4e4e1bd053a5cc7 to your computer and use it in GitHub Desktop.
XML Deserializer throwing Exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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