class Common
{
/// <summary> /// 将XML文件写入指定的对象 /// </summary> /// <param name="xmlFile">xml绝对路径</param> /// <param name="type">序列的类型,要与XML对应的类</param> /// <returns>将对象返回,当文件操作失败则返回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">数据源对象</param> /// <param name="xmlFile">目标路径</param> /// <param name="type">转换类型</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);
}
}
}