前段时间做object转换xml想了很多,所有打算整理下 做成以下的通用方法。
public static bool ObjectToXml<T>(string filePath, T t)
{
XmlSerializer xs = new XmlSerializer(typeof(P2PRestoreInfo));
Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
xs.Serialize(stream, t);
stream.Close();
return false;
}
public static T XmlToObject<T>(string filePath)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
T p2p = (T)xs.Deserialize(stream);
stream.Close();
return p2p;
}