PageRenderTime 32ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/FlashCard/WindowsFormsApplication1/Utility.cs

https://bitbucket.org/floAr/personal
C# | 47 lines | 45 code | 2 blank | 0 comment | 0 complexity | 6ed0a27f2c02176aa7611b7c57efaa3f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6. using System.Xml;
  7. using System.IO;
  8. using System.Diagnostics;
  9. using System.Text.RegularExpressions;
  10. namespace FlashCard
  11. {
  12. public static class Utility
  13. {
  14. public static void Serialize<T>(T obj, string path, params Type[] knownTypes)
  15. {
  16. using (FileStream stream = new FileStream(path, FileMode.Create))
  17. {
  18. using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
  19. {
  20. try
  21. {
  22. writer.Formatting = Formatting.Indented;
  23. DataContractSerializer ser = new DataContractSerializer(typeof(T), knownTypes);
  24. ser.WriteObject(writer, obj);
  25. }
  26. catch (Exception e)
  27. {
  28. Debug.Fail(e.ToString());
  29. }
  30. }
  31. }
  32. }
  33. public static T Deserialize<T>(string path, params Type[] knownTypes)
  34. {
  35. using (FileStream stream = new FileStream(path, FileMode.Open))
  36. {
  37. using (XmlTextReader reader = new XmlTextReader(stream))
  38. {
  39. DataContractSerializer serializer = new DataContractSerializer(typeof(T), knownTypes);
  40. return (T)serializer.ReadObject(reader, true);
  41. }
  42. }
  43. }
  44. }
  45. }