Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FileManager
- {
- public void save<T>(T serializableObject, String path, String fileName)
- {
- if (serializableObject == null) { return; }
- try
- {
- XmlDocument xmlDocument = new XmlDocument();
- XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
- using (MemoryStream stream = new MemoryStream())
- {
- serializer.Serialize(stream, serializableObject);
- stream.Position = 0;
- xmlDocument.Load(stream);
- xmlDocument.Save(path + fileName);
- stream.Close();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error while saving " + fileName);
- Console.WriteLine("*** ERROR MESSAGE ***");
- Console.WriteLine(ex.Message);
- }
- }
- public T load<T>(String path, String fileName)
- {
- if (string.IsNullOrEmpty(fileName)) { return default(T); }
- T objectOut = default(T);
- try
- {
- string attributeXml = string.Empty;
- XmlDocument xmlDocument = new XmlDocument();
- xmlDocument.Load(path + fileName);
- string xmlString = xmlDocument.OuterXml;
- using (StringReader read = new StringReader(xmlString))
- {
- Type outType = typeof(T);
- XmlSerializer serializer = new XmlSerializer(outType);
- using (XmlReader reader = new XmlTextReader(read))
- {
- objectOut = (T)serializer.Deserialize(reader);
- reader.Close();
- }
- read.Close();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error while loading " + fileName);
- Console.WriteLine("*** ERROR MESSAGE ***");
- Console.WriteLine(ex.Message);
- }
- return objectOut;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement