using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace ZAS.WGT.Common
{
public class XmlHelper
{
public static bool SaveToXml<T>(string filePath, T data) where T : class, new()
{
try
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
XmlSerializer serializer = new XmlSerializer(data.GetType());
FileStream fs = File.Open(filePath, FileMode.OpenOrCreate);
using (XmlWriter xmlWriter = XmlWriter.Create(fs, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, data);
}
fs.Close();
return true;
}
catch (Exception)
{
return false;
}
}
public static T ReadFromXml<T>(string filePath) where T:class,new()
{
T data = new T();
try
{
XmlSerializer serializer = new XmlSerializer(data.GetType());
FileStream fs = File.Open(filePath, FileMode.Open);
data = (T)serializer.Deserialize(fs);
fs.Close();
return data;
}
catch
{
return data;
}
}
}
}