jellyfin/MediaBrowser.Common/Serialization/XmlSerializer.cs

42 lines
1.2 KiB
C#
Raw Normal View History

using System.IO;
2012-08-13 07:51:42 -07:00
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)
{
GetSerializer<T>().Serialize(stream, obj);
2012-08-13 07:51:42 -07:00
}
public static void SerializeToFile<T>(T obj, string file)
{
using (FileStream stream = new FileStream(file, FileMode.Create))
2012-08-13 07:51:42 -07:00
{
GetSerializer<T>().Serialize(stream, obj);
2012-08-13 07:51:42 -07:00
}
}
public static T DeserializeFromFile<T>(string file)
{
using (Stream stream = File.OpenRead(file))
{
return (T)GetSerializer<T>().Deserialize(stream);
2012-08-13 07:51:42 -07:00
}
}
public static T DeserializeFromStream<T>(Stream stream)
{
return (T)GetSerializer<T>().Deserialize(stream);
2012-08-13 07:51:42 -07:00
}
private static System.Xml.Serialization.XmlSerializer GetSerializer<T>()
2012-08-13 07:51:42 -07:00
{
return new System.Xml.Serialization.XmlSerializer(typeof(T));
2012-08-13 07:51:42 -07:00
}
}
}