Skip to content

Instantly share code, notes, and snippets.

@chuwilliamson
Created March 24, 2016 19:33
Show Gist options
  • Save chuwilliamson/892c28a056e4296729df to your computer and use it in GitHub Desktop.
Save chuwilliamson/892c28a056e4296729df to your computer and use it in GitHub Desktop.
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