XmlSerializer.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System.IO;
  2. namespace MediaBrowser.Common.Serialization
  3. {
  4. /// <summary>
  5. /// Provides a wrapper around third party xml serialization.
  6. /// </summary>
  7. public class XmlSerializer
  8. {
  9. public static void SerializeToStream<T>(T obj, Stream stream)
  10. {
  11. GetSerializer<T>().Serialize(stream, obj);
  12. }
  13. public static void SerializeToFile<T>(T obj, string file)
  14. {
  15. using (FileStream stream = new FileStream(file, FileMode.Create))
  16. {
  17. GetSerializer<T>().Serialize(stream, obj);
  18. }
  19. }
  20. public static T DeserializeFromFile<T>(string file)
  21. {
  22. using (Stream stream = File.OpenRead(file))
  23. {
  24. return (T)GetSerializer<T>().Deserialize(stream);
  25. }
  26. }
  27. public static T DeserializeFromStream<T>(Stream stream)
  28. {
  29. return (T)GetSerializer<T>().Deserialize(stream);
  30. }
  31. private static System.Xml.Serialization.XmlSerializer GetSerializer<T>()
  32. {
  33. return new System.Xml.Serialization.XmlSerializer(typeof(T));
  34. }
  35. }
  36. }