/// <summary>
/// 读取XML文件,反序列化为对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="t">对象</param>
/// <param name="fileName">xml文件名</param>
/// <returns>反序列化的对象</returns>
public static T ReadXMLFileToType<T>(T t, string xmlFileName)
{
XmlSerializer xs = new XmlSerializer(t.GetType());
XmlTextReader reader = new XmlTextReader(xmlFileName);
reader.Normalization = false;
t = (T)xs.Deserialize(reader);
reader.Close();
return t;
}
/// <summary>
/// 序列化对象,保存为XML文件,前缀为空
/// </summary>
/// <param name="t">对象类型</param>
/// <param name="s">对象序列化后的Xml文件</param>
public static void SaveTypeToXmlFile<T>(T t, string xmlFile)
{
XmlSerializer xs = new XmlSerializer(t.GetType());
TextWriter writer = new StreamWriter(xmlFile);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(String.Empty, String.Empty);
xs.Serialize(writer, t, ns);
writer.Flush();
writer.Close();
}
}