2
0

HelperFile.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // UtilData.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using System.IO;
  14. using System.Xml;
  15. using System.Xml.Serialization;
  16. using RobotGameData;
  17. #endregion
  18. namespace RobotGameData.Helper
  19. {
  20. /// <summary>
  21. /// Useful functions about file system.
  22. /// </summary>
  23. public static class HelperFile
  24. {
  25. /// <summary>
  26. /// It saves the specified object’s data into a XML file.
  27. /// </summary>
  28. public static void SaveData(string fileName, object data)
  29. {
  30. string path = fileName;
  31. Stream stream = File.Create(path);
  32. // Convert the object to XML data and put it in the stream
  33. XmlSerializer serializer = new XmlSerializer(data.GetType());
  34. serializer.Serialize(stream, data);
  35. // Close the file
  36. stream.Close();
  37. }
  38. /// <summary>
  39. /// It reads from a XML file into the specified type class.
  40. /// </summary>
  41. public static object LoadData(string fileName, Type type)
  42. {
  43. string path = fileName;
  44. Stream stream = File.OpenRead(Path.Combine("Content", path));
  45. XmlSerializer serializer = new XmlSerializer(type);
  46. return serializer.Deserialize(stream);
  47. }
  48. }
  49. }