Created
March 24, 2016 19:33
-
-
Save chuwilliamson/892c28a056e4296729df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Xml.Serialization; | |
namespace MarvelRPG | |
{ | |
public static class Utilities | |
{ | |
public static void SerializeXML<T>(string s, T t, string path) | |
{ | |
if (Directory.Exists(path)) | |
{ | |
using (FileStream fs = File.Create(path + s + ".xml")) | |
{ | |
XmlSerializer serializer = new XmlSerializer(typeof(T)); | |
serializer.Serialize(fs, t); | |
fs.Close(); | |
} | |
} | |
else | |
{ | |
string appName = System.Windows.Forms.Application.ProductName; | |
Directory.CreateDirectory(path + appName); | |
} | |
} | |
/// <summary> | |
/// deserialize from a path | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="s"></param> | |
/// <returns></returns> | |
public static T DeserializeXML<T>(string s) | |
{ | |
T t; //We will use the as the return value | |
using (FileStream fs = File.OpenRead(s + ".xml")) | |
{ | |
XmlSerializer deserializer = new XmlSerializer(typeof(T)); | |
t = (T)deserializer.Deserialize(fs); | |
fs.Close(); | |
} | |
return t; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment