dmdb/model/XmlData.cs

88 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace dezentrale.model
{
public class XmlData
{
[XmlElement] public DateTime LastChanged { get; set; } = DateTime.Now;
[XmlIgnore]
private static Type[] SerializeTypes =
{
typeof(bool),
typeof(uint),
typeof(int),
typeof(string),
typeof(DateTime),
typeof(Configuration),
typeof(ConfigSmtp),
typeof(ConfigVSMail),
typeof(FormMail),
typeof(LogEntry),
typeof(Member),
typeof(Member.ePaymentClass),
typeof(Member.eRole),
typeof(Member.eStatus),
typeof(Member.eType),
typeof(MemberList),
typeof(MemberPayment),
//typeof(List<KeyValue>),
//typeof(List<TaskLogEntry>),
//typeof(List<ListColumn>),
};
public static XmlData LoadFromFile(string fileName)
{
FileStream fs = null;
try
{
XmlSerializer ser = new XmlSerializer(typeof(XmlData), XmlData.SerializeTypes);
fs = new FileStream(fileName, FileMode.Open);
XmlData ds = (XmlData)ser.Deserialize(fs);
fs.Close();
return ds;
}
catch (Exception e)
{
if (fs != null) fs.Close();
if (e.InnerException != null) throw e.InnerException; else throw e;
}
}
public static bool SaveToFile(string fileName, XmlData ds)
{
try
{
if (File.Exists(fileName))
{
if (File.Exists(fileName + ".bak")) File.Delete(fileName + ".bak");
File.Move(fileName, fileName + ".bak");
}
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer ser = new XmlSerializer(typeof(XmlData), XmlData.SerializeTypes);
FileStream fs;
fs = new FileStream(fileName, FileMode.Create);
ser.Serialize(fs, ds, ns);
fs.Close();
}
catch (Exception e)
{
if (e.InnerException != null)
throw e.InnerException;
else
throw e;
}
return true;
}
}
}