/// <summary>Writes object data to xml file.</summary> /// <param name="file">Xml file name.</param> /// <param name="xObj">T object.</param> public static bool WriteFile(string file, T xObj) { bool ok = false; var serializer = new XObjectSerializer <T>(); try { string xml = serializer.Serialize(xObj).OuterXml; using (var writer = new StreamWriter(file, false)) { writer.Write(xml.Trim()); writer.Flush(); writer.Close(); } ok = true; } catch { } return(ok); }
/// <summary>Reads object data from a xml file.</summary> /// <param name="file">Xml file name.</param> /// <returns>T object</returns> public static T ReadFile(string file) { T xObj; var serializer = new XObjectSerializer <T>(); try { string xml; using (var reader = new StreamReader(file)) { xml = reader.ReadToEnd(); reader.Close(); } xObj = serializer.Deserialize(xml); return(xObj); } catch { } xObj = new T(); xObj.Init(); return(xObj); }