public static T DeSerializeObject <T>(string fileName) { if (string.IsNullOrEmpty(fileName)) { return(default(T)); } T objectOut = default(T); try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(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); } } } catch (Exception ex) { Console.Beep(); DialogError.Error("" + ex); } return(objectOut); }
//**************************************************************************************************************** #endregion //**************************************************************************************************************** #region Log private static void Log(string p_string, string p_fileName) { try { File.AppendAllText(Path.Combine(C_Variables.Path_.dir_home, p_fileName), p_string + Environment.NewLine); } catch (Exception e) { DialogError.Error(e.StackTrace); } }
//**************************************************************************************************************** #region Serialize public static void SerializeObject <T>(T serializableObject, 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(fileName); } } catch (Exception ex) { DialogError.Error("" + ex); } }