XmlHelper
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace System.Bwch.XmlConfig
{
/**//// <summary>
/// 读取XML配置文件类
/// </summary>
public class XmlHelper
{
private string strXmlPath = ""; //Xml文档路径
private XmlDocument xmlDoc; //XML文档
/**//// <summary>
/// 初始化ReadXml类
/// </summary>
/// <param name="XMLPath">XML文件路径</param>
public XmlHelper(string XMLPath)
{
if (!System.IO.File.Exists(XMLPath))
{
throw new Exception("没有找到指定的路径:" + XMLPath + "的XML文档");
}
strXmlPath = XMLPath;
xmlDoc = new XmlDocument();
xmlDoc.Load(XMLPath);
}
/**//// <summary>
/// 读取XML文件指定键值的value值
/// </summary>
/// <param name="XMLNodePath">键值的路径,格式为(根节点/节点/子节点)</param>
/// <param name="valueName">指定键值的属性名称</param>
/// <returns>value值</returns>
public string ReadXmlValue(string XMLNodePath,string valueName)
{
try
{
XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XMLNodePath);
return xml.GetAttribute(valueName);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/**//// <summary>
/// 写XML指定节点的
/// </summary>
/// <param name="XmlNodePath">键值路径,格式为((根节点/节点/子节点))</param>
/// <param name="valueName">属性名称</param>
/// <param name="Value">属性</param>
/// <returns></returns>
public bool WriteXmlValue(string XmlNodePath,string valueName, string Value)
{
try
{
XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath);
xml.SetAttribute(valueName, Value); //设置
xmlDoc.Save(strXmlPath); //保存
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/**//// <summary>
/// 读取XML键值
/// </summary>
/// <param name="XmlNodePath">键值路径,格式为((根节点/节点/子节点))</param>
/// <returns></returns>
public string ReadXmlKey(string XmlNodePath)
{
try
{
XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath);
return xml.InnerText;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/**//// <summary>
/// 写XML键值
/// </summary>
/// <param name="XmlNodePath">键值路径,格式为((根节点/节点/子节点))</param>
/// <param name="Value">值</param>
/// <returns></returns>
public bool WriteXmlKey(string XmlNodePath, string Value)
{
try
{
XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath);
xml.InnerText=Value;
xmlDoc.Save(strXmlPath);
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace System.Bwch.XmlConfig
{
/**//// <summary>
/// 读取XML配置文件类
/// </summary>
public class XmlHelper
{
private string strXmlPath = ""; //Xml文档路径
private XmlDocument xmlDoc; //XML文档
/**//// <summary>
/// 初始化ReadXml类
/// </summary>
/// <param name="XMLPath">XML文件路径</param>
public XmlHelper(string XMLPath)
{
if (!System.IO.File.Exists(XMLPath))
{
throw new Exception("没有找到指定的路径:" + XMLPath + "的XML文档");
}
strXmlPath = XMLPath;
xmlDoc = new XmlDocument();
xmlDoc.Load(XMLPath);
}
/**//// <summary>
/// 读取XML文件指定键值的value值
/// </summary>
/// <param name="XMLNodePath">键值的路径,格式为(根节点/节点/子节点)</param>
/// <param name="valueName">指定键值的属性名称</param>
/// <returns>value值</returns>
public string ReadXmlValue(string XMLNodePath,string valueName)
{
try
{
XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XMLNodePath);
return xml.GetAttribute(valueName);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/**//// <summary>
/// 写XML指定节点的
/// </summary>
/// <param name="XmlNodePath">键值路径,格式为((根节点/节点/子节点))</param>
/// <param name="valueName">属性名称</param>
/// <param name="Value">属性</param>
/// <returns></returns>
public bool WriteXmlValue(string XmlNodePath,string valueName, string Value)
{
try
{
XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath);
xml.SetAttribute(valueName, Value); //设置
xmlDoc.Save(strXmlPath); //保存
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/**//// <summary>
/// 读取XML键值
/// </summary>
/// <param name="XmlNodePath">键值路径,格式为((根节点/节点/子节点))</param>
/// <returns></returns>
public string ReadXmlKey(string XmlNodePath)
{
try
{
XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath);
return xml.InnerText;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/**//// <summary>
/// 写XML键值
/// </summary>
/// <param name="XmlNodePath">键值路径,格式为((根节点/节点/子节点))</param>
/// <param name="Value">值</param>
/// <returns></returns>
public bool WriteXmlKey(string XmlNodePath, string Value)
{
try
{
XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath);
xml.InnerText=Value;
xmlDoc.Save(strXmlPath);
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}