Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static bool ReadFile<T>(String name, ref T toBeSet)
- {
- FileStream xmlStream = null;
- try
- {
- XmlSerializer xmlSer = new XmlSerializer(typeof(T));
- xmlStream = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.None);
- toBeSet = (T)xmlSer.Deserialize(xmlStream);
- return true;
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- return false;
- }
- finally
- {
- if (xmlStream != null)
- xmlStream.Close();
- }
- }
- public static bool WriteFile<T>(String name, T toWrite)
- {
- FileStream xmlStream = null;
- try
- {
- XmlSerializer xmlSer = new XmlSerializer(typeof(T));
- xmlStream = new FileStream(name, FileMode.Create, FileAccess.Write, FileShare.None);
- xmlSer.Serialize(xmlStream, toWrite);
- return true;
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- return false;
- }
- finally
- {
- if (xmlStream != null)
- xmlStream.Close();
- }
- }
- Used as:
- class Blah
- {
- public String Name;
- public String Value;
- }
- void saveBlah()
- {
- Blah blah = new Blah();
- blah.Name = "blah";
- blah.Value = "1";
- if(!WriteFile<Blah>("blah.xml",blah)
- {
- Console.WriteLine("Failed!");
- }
- }
Add Comment
Please, Sign In to add comment