XML与Object的范型转换

前段时间做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;
}

posted @ 2014-04-08 10:52  丰叔(◕ˇ◞◟ˇ◕)  阅读(154)  评论(0编辑  收藏  举报