using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
namespace FindWordIsUsed.ClassCode
{
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;
}
if (!serverFilePath.EndsWith("//"))
serverFilePath += "//";
if (!xmlFileName.EndsWith(".xml"))
xmlFileName += ".xml";
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)
{
if (!serverFilePath.EndsWith("//"))
serverFilePath += "//";
if (!xmlFileName.EndsWith(".xml"))
xmlFileName += ".xml";
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)
{
if (dataSet.DataSetName != dataSetName)
dataSet.DataSetName = dataSetName;
if (!serverFilePath.EndsWith("//"))
serverFilePath += "//";
if (!xmlFileName.EndsWith(".xml"))
xmlFileName += ".xml";
serverFilePath += xmlFileName;
//SuceedFailMessage isSuceed = new SuceedFailMessage();
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)
{
if (!serverFilePath.EndsWith("//"))
serverFilePath += "//";
if (!xmlFileName.EndsWith(".xml"))
xmlFileName += ".xml";
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;
}
}
#endregion
internal static DataSet ReadDataSetFromXml(string fileName)
{
bool isExistsPath = File.Exists(fileName);
DataSet dataSet = new DataSet();
try
{
if (isExistsPath)
{
dataSet.ReadXml(fileName);
}
else
{
dataSet = null;
}
return dataSet;
}
catch (IOException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
}
}