From 099ac83cc32033fceca9371d2969bab16f28b6ca Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Mon, 13 Aug 2012 10:51:42 -0400 Subject: [PATCH] Added an XmlSerializer to common --- .../MediaBrowser.Common.csproj | 1 + .../Serialization/XmlSerializer.cs | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 MediaBrowser.Common/Serialization/XmlSerializer.cs diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index dc9d807f94..25a855c389 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -77,6 +77,7 @@ + Splash.xaml diff --git a/MediaBrowser.Common/Serialization/XmlSerializer.cs b/MediaBrowser.Common/Serialization/XmlSerializer.cs new file mode 100644 index 0000000000..924a7e8f41 --- /dev/null +++ b/MediaBrowser.Common/Serialization/XmlSerializer.cs @@ -0,0 +1,50 @@ +using System; +using System.IO; + +namespace MediaBrowser.Common.Serialization +{ + /// + /// Provides a wrapper around third party xml serialization. + /// + public class XmlSerializer + { + public static void SerializeToStream(T obj, Stream stream) + { + ServiceStack.Text.XmlSerializer.SerializeToStream(obj, stream); + } + + public static void SerializeToFile(T obj, string file) + { + using (StreamWriter streamWriter = new StreamWriter(file)) + { + ServiceStack.Text.XmlSerializer.SerializeToWriter(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(string file) + { + using (Stream stream = File.OpenRead(file)) + { + return ServiceStack.Text.XmlSerializer.DeserializeFromStream(stream); + } + } + + public static T DeserializeFromStream(Stream stream) + { + return ServiceStack.Text.XmlSerializer.DeserializeFromStream(stream); + } + + public static T DeserializeFromString(string data) + { + return ServiceStack.Text.XmlSerializer.DeserializeFromString(data); + } + } +}