WinForm XML文件数据读取,写入,删除XML文件
数据写入XML
public static void WriteXML<T>(List<T> overview, string path)
{
FileInfo fInfo = new FileInfo(path);
if (!fInfo.Directory.Exists)
{
fInfo.Directory.Create();
}
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(typeof(List<T>));
System.IO.FileStream file = System.IO.File.Create(path);
writer.Serialize(file, overview);
file.Close();
}
从XML读取数据
public static List<T> ReadXML<T>(T test,string path)
{
System.Xml.Serialization.XmlSerializer reader =
new System.Xml.Serialization.XmlSerializer(typeof(List<T>));
System.IO.StreamReader file = new System.IO.StreamReader(path);
List<T> overview = (List<T>)reader.Deserialize(file);
file.Close();
return overview;
}
/// <summary>
/// 删除本地XML文件
/// </summary>
public static void DeleteLocalityFile()
{
try
{
string Filepath = "D:\\DataTemp\\"; //Xml文件存放路径
if (System.IO.Directory.Exists(Filepath))
{
// 获得文件数组
string[] strFiles = System.IO.Directory.GetFiles(Filepath);
foreach (string strFile in strFiles)
{
// 删除文件
System.IO.File.Delete(strFile);
}
}
}
catch (Exception ex)
{
throw ex;
}
}