class Common
{
/// <summary> /// 將XML文件寫入指定的對象 /// </summary> /// <param name="xmlFile">xml絕對路徑</param> /// <param name="type">序列的類型,要與XML對應(yīng)的類</param> /// <returns>將對象返回,當(dāng)文件操作失敗則返回Null值</returns> public static object DeserializeXmlToObject(
string xmlFile, Type type)
{
XmlSerializer mySerializer =
new XmlSerializer(type);
using (FileStream stream =
new FileStream(xmlFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
return mySerializer.Deserialize(stream);
}
}
/// <summary> /// 將對象寫入到XML中 /// </summary> /// <param name="obj">數(shù)據(jù)源對象</param> /// <param name="xmlFile">目標(biāo)路徑</param> /// <param name="type">轉(zhuǎn)換類型</param> public static void SerializeObjectToXml(
object obj, String xmlFile, Type type)
{
XmlSerializer mySerializer =
new XmlSerializer(type);
using (FileStream stream =
new FileStream(xmlFile, FileMode.Create, FileAccess.Write, FileShare.Read))
{
mySerializer.Serialize(stream, obj);
}
}
}