您的位置:首页 > 编程语言 > C#

【工具类】-Xml配置文件读写操作类

2017-05-24 16:33 459 查看
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.IO;
using System.Xml;

public class ManageXML
{
/// <summary>
/// 返回用户操作的SQL语句
/// </summary>
/// <param name="sqlNodeName">XML的操作节点</param>
/// <returns>操作的sql语句</returns>
public static string GetXMLPath(string strXMlFileName)
{
string m_strFullPath = "";
Assembly Asm = Assembly.GetExecutingAssembly();
//获取文件的路径
//m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + "XMLSql.xml";
m_strFullPath = AppDomain.CurrentDomain.BaseDirectory + "XMLLibrary\\" + strXMlFileName;
return m_strFullPath;
}
/// <summary>
/// 获取XML数据库中的数据的方法
/// </summary>
/// <param name="strFilePath">传入文件路径</param>
/// <returns>返回一个数据集</returns>
public static DataSet GetAllDataFromXML(string strFilePath)
{
DataSet ds = new DataSet();
FileInfo fileInfo = new FileInfo(strFilePath);
if (fileInfo.Exists)
{
try
{
ds.ReadXml(strFilePath);
}
catch { }
}
else
{
ds = null;
}
if (ds != null)
{
if (ds.Tables[0].Rows.Count < 1)
ds = null;
}
return ds;
}

/// <summary>
/// 获取指定目录下所有子节点的值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeDir">节点目录</param>
/// <returns></returns>
public static  Hashtable GetNodeList(string strFileName, string nodeDir)
{
Hashtable strNodeList = new Hashtable();
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);

XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点

foreach (XmlNode xn in nodeList)     //遍历所有子节点
{
XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型
strNodeList.Add(xe.GetAttribute("id").ToString(), xe.InnerText.Trim());
}

}
catch (Exception)
{

throw;
}
return strNodeList;
}

/// <summary>
/// 获取指定节点的值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
public static string GetNodeValue(string strFileName, string nodeName, string nodeDir)
{
string value = null;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);

XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点

foreach (XmlNode xn in nodeList)    //遍历所有子节点
{
XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型

if (xe.Name == nodeName)
{
value = xe.InnerText.Trim();

break;
}
}
}
catch (Exception exp)
{
throw exp;
}

return value;
}

/// <summary>
/// 获取指定节点下面对应属性的值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <param name="attribute">节点对应的属性名称</param>
/// <returns></returns>
public static string GetNodeValue(string strFileName, string nodeName, string nodeDir, string attribute)
{
string value = null;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);

XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点

foreach (XmlNode xn in nodeList)    //遍历所有子节点
{
XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型

if (xe.Name == nodeName)
{
//value = xe.InnerText.Trim();
value = (xe).Attributes[attribute].Value;
break;
}
}
}
catch (Exception exp)
{
throw exp;
}

return value;
}

/// <summary>
/// 修改指定结点值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
public static bool UpdateNoteValue(string strFileName, string nodeName, string value, string nodeDir)
{
bool isSucceed = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);

XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点

foreach (XmlNode xn in nodeList)    //遍历所有子节点
{
XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型

if (xe.Name == nodeName)
{
xe.InnerText = value;

isSucceed = true;
break;
}
}

xmlDoc.Save(strFileName);
}
catch (Exception exp)
{
throw exp;
}

return isSucceed;
}

/// <summary>
/// 修改指定结点值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
public static bool UpdateNoteValue(string strFileName, string nodeName, string value, string nodeDir, string attribute, string attributeValue)
{
bool isSucceed = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);

XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点

foreach (XmlNode xn in nodeList)    //遍历所有子节点
{
XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型

if (xe.Name == nodeName)
{
xe.InnerText = value;
(xe).Attributes[attribute].Value = attributeValue;
isSucceed = true;
break;
}
}

xmlDoc.Save(strFileName);
}
catch (Exception exp)
{
throw exp;
}

return isSucceed;
}

/// <summary>
/// 修改指定结点值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
public static bool UpdateNoteValue(string strFileName, Hashtable hstable, string nodeDir)
{
bool isSucceed = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);

XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
foreach (DictionaryEntry item in hstable)
{
foreach (XmlNode xn in nodeList)    //遍历所有子节点
{
XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型

if (xe.Name == item.Key.ToString())
{
xe.InnerText = item.Value.ToString();

isSucceed = true;
break;
}
}
}

xmlDoc.Save(strFileName);
}
catch (Exception exp)
{
throw exp;
}

return isSucceed;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  工具类 c#