Created
February 7, 2013 14:46
-
-
Save LaptopHeaven/4731352 to your computer and use it in GitHub Desktop.
Simple extension method to write and load data to xml. There are lots of caveats to this code, like all items in class T need to be Serializable.
This file contains 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; | |
public static class XmlTools | |
{ | |
public static void SaveAsXml<T>(T data, string filename) | |
{ | |
var serializer = new XmlSerializer(typeof(T)); | |
using (var w = new StreamWriter(filename)) | |
{ | |
serializer.Serialize(w, data); | |
} | |
} | |
public static T LoadXml<T>(string filename) where T : class | |
{ | |
T data; | |
var serializer = new XmlSerializer(typeof(T)); | |
using (var r = new StreamReader(filename)) | |
{ | |
data = (T)serializer.Deserialize(r); | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment