Skip to content

Instantly share code, notes, and snippets.

@lavn0
Last active April 11, 2017 12:28
Show Gist options
  • Select an option

  • Save lavn0/798d72ce7a2fd86050d6453aab896039 to your computer and use it in GitHub Desktop.

Select an option

Save lavn0/798d72ce7a2fd86050d6453aab896039 to your computer and use it in GitHub Desktop.
DataContractParser
// add reference to System.Runtime.Serialization, Version=4.0.0.0
// add reference to System.XML, Version=4.0.0.0
using System.IO;
using System.Runtime.Serialization;
using System.Text;
public static class DataContractParser
{
public static T ReadObject<T>(Stream stream)
{
return (T)new DataContractSerializer(typeof(T)).ReadObject(stream);
}
public static T ReadObject<T>(string fileName)
{
using (var sr = new StreamReader(fileName))
{
return ReadObject<T>(sr.BaseStream);
}
}
public static void WriteObject<T>(T obj, Stream stream)
{
new DataContractSerializer(typeof(T)).WriteObject(stream, obj);
}
public static void WriteObject<T>(T obj, string fileName)
{
using (var sw = new StreamWriter(fileName))
{
WriteObject<T>(obj, sw.BaseStream);
}
}
public static T ParseObject<T>(string xmlString)
{
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
{
return ReadObject<T>(ms);
}
}
public static string ParseObject<T>(T obj)
{
MemoryStream ms = null;
try
{
ms = new MemoryStream();
WriteObject<T>(obj, ms);
ms.Seek(0, SeekOrigin.Begin);
using (var sr = new StreamReader(ms))
{
ms = null;
return sr.ReadToEnd();
}
}
finally
{
ms?.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment