using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml.Serialization;
namespace Dal
{
public static class XmlHelper
{
#region Xml DataTable文件的操作
/// <summary>
/// 将表中数据保存到文件夹folderName下的xml文件xmlName;
/// </summary>
/// <param name="xmlName">要保存的xml文件名称</param>
/// <param name="folderName">文件所在路径,但是并不包括文件名称(Application.StartupPath + Path.DirectorySeparatorChar + "FolderName" + Path.DirectorySeparatorChar;)</param>
/// <param name="dataTable">要保存的数据表</param>
/// <returns>保存成功返回true,否则是false</returns>
public static bool WriteTableToXml(string xmlFileName, string serverFilePath, DataTable dataTable, string tableName)
{
if (dataTable.TableName != tableName)
{
dataTable.TableName = tableName;
}
serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
try
{
string directoryName = Path.GetDirectoryName(serverFilePath);//获取文件所在目录
bool isExists = Directory.Exists(directoryName);//检查该目录是否存在
if (!isExists)//不存在该文件目录,则创建
{
Directory.CreateDirectory(Path.GetDirectoryName(serverFilePath));
}
dataTable.WriteXml(serverFilePath, XmlWriteMode.WriteSchema);
return true;
}
catch
{
return false; ;
}
}
/// <summary>
/// 从xml文件中读数据,返回一个DataTable(无论是XML文件中包含的是DataSet,还是单独的表)
/// </summary>
/// <param name="xmlName">xml文件名称</param>
/// <param name="serverFilePath">xml文件所在路径</param>
/// <returns>DataTable</returns>
public static DataTable ReadTableFromXml(string serverFilePath, string xmlFileName, string tableName)
{
serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
bool isExistsPath = File.Exists(serverFilePath);
DataTable dataTable = new DataTable(tableName);
try
{
if (isExistsPath)
{
dataTable.ReadXml(serverFilePath);
}
else
{
dataTable = null;
}
return dataTable;
}
catch (IOException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region DataSet XML file operate
public static bool WriteDataSetToXml(string xmlFileName, string serverFilePath, DataSet dataSet, string dataSetName)
{
serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
try
{
if (!Directory.Exists(Path.GetDirectoryName(serverFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(serverFilePath));
}
dataSet.WriteXml(serverFilePath, XmlWriteMode.WriteSchema);
return true;
}
catch
{
return false;
}
}
public static DataSet ReadDataSetFromXml(string serverFilePath, string xmlFileName, string dataSetName)
{
serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
bool isExistsPath = File.Exists(serverFilePath);
DataSet dataSet = new DataSet(dataSetName);
try
{
if (isExistsPath)
{
dataSet.ReadXml(serverFilePath);
}
else
{
dataSet = null;
}
return dataSet;
}
catch (IOException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
public static void SerializerXml<T>(string serverFilePath, string xmlFileName, T t)
{
serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
XmlSerializer xs = new XmlSerializer(typeof(T));
using (Stream stream = new FileStream(serverFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
xs.Serialize(stream, t);
stream.Close();
}
}
public static T DeserializeXml<T>(string serverFilePath, string xmlFileName)
{
serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
XmlSerializer xs = new XmlSerializer(typeof(T));
using (Stream stream = new FileStream(serverFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
T p = (T)xs.Deserialize(stream);
return p;
}
}
private static string GetFileFullName(string serverFilePath, string xmlFileName)
{
if (!serverFilePath.EndsWith("//"))
{
serverFilePath += "//";
}
if (!xmlFileName.EndsWith(".xml"))
{
xmlFileName += ".xml";
}
serverFilePath += xmlFileName;
return serverFilePath;
}
#endregion
}
}