Added an XmlSerializer to common

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti 2012-08-13 10:51:42 -04:00
parent 02689bece4
commit 099ac83cc3
2 changed files with 51 additions and 0 deletions

View File

@ -77,6 +77,7 @@
<Compile Include="Net\StreamExtensions.cs" />
<Compile Include="Plugins\BasePlugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Serialization\XmlSerializer.cs" />
<Compile Include="UI\Splash.xaml.cs">
<DependentUpon>Splash.xaml</DependentUpon>
</Compile>

View File

@ -0,0 +1,50 @@
using System;
using System.IO;
namespace MediaBrowser.Common.Serialization
{
/// <summary>
/// Provides a wrapper around third party xml serialization.
/// </summary>
public class XmlSerializer
{
public static void SerializeToStream<T>(T obj, Stream stream)
{
ServiceStack.Text.XmlSerializer.SerializeToStream(obj, stream);
}
public static void SerializeToFile<T>(T obj, string file)
{
using (StreamWriter streamWriter = new StreamWriter(file))
{
ServiceStack.Text.XmlSerializer.SerializeToWriter<T>(obj, streamWriter);
}
}
public static object DeserializeFromFile(Type type, string file)
{
using (Stream stream = File.OpenRead(file))
{
return ServiceStack.Text.XmlSerializer.DeserializeFromStream(type, stream);
}
}
public static T DeserializeFromFile<T>(string file)
{
using (Stream stream = File.OpenRead(file))
{
return ServiceStack.Text.XmlSerializer.DeserializeFromStream<T>(stream);
}
}
public static T DeserializeFromStream<T>(Stream stream)
{
return ServiceStack.Text.XmlSerializer.DeserializeFromStream<T>(stream);
}
public static T DeserializeFromString<T>(string data)
{
return ServiceStack.Text.XmlSerializer.DeserializeFromString<T>(data);
}
}
}