http://xingfustar.cnblogs.com
我的问题是如何能得到根节点的名字,而不是像现在这样写死为“Configuration"。希望大家多多讨论,提出宝贵意见
/*
----------------------------------------------------------------
* 版权: http://XingFuStar.cnblogs.com
*
* 文件名: XmlConfig
* 文件功能描述: 简单的XML配置文件读写类
*
* 作者:XingFuStar
* 日期:2007年9月13日
*
* 当前版本:V1.0.0
*
* 修改日期:
* 修改内容:
*--------------------------------------------------------------- */
/* ----------------------------------------------------------------
* 相关的XML文件模版
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<Section1>
<Key1>Value</Key1>
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
<KeyN>Value</KeyN>
</Section1>
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
<SectionN>
<Key1>Value</Key1>
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
<KeyN>Value</KeyN>
</SectionN>
</Configuration>
*---------------------------------------------------------------- */
using System;
using System.Xml;
using System.Diagnostics;
namespace XingFuStudio.Config
{
class XmlConfig
{
private string xmlPath;
private bool isConfig;
private XmlDocument xmlDocument;
/// <summary>
/// 构造函数:装载配置文件
/// </summary>
/// <param name="xmlPath"> 配置文件的路径 </param>
public XmlConfig( string xmlPath)
{
// 请不要删除以下信息
// 版权: http://XingFuStar.cnblogs.com
this .xmlDocument = new XmlDocument();
this .XmlPath = xmlPath;
}
/// <summary>
/// Xml配置文件的路径
/// </summary>
public string XmlPath
{
set
{
xmlPath = value;
isConfig = OnXmlPathChange();
}
}
/// <summary>
/// 读取配置
/// </summary>
/// <param name="section"> 节点 </param>
/// <param name="key"> 键 </param>
/// <param name="value"> 返回的键值 </param>
/// <returns> 键值 </returns>
/// <returns> 是否读取成功 </returns>
public bool ReadConfig( string section, string key, ref string value)
{
bool isRead = false ;
try
{
if (isConfig)
{
value = xmlDocument.SelectSingleNode( " Configuration " ).SelectSingleNode(section).SelectSingleNode(key).InnerText;
isRead = true ;
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isRead;
}
/// <summary>
/// 写入配置
/// </summary>
/// <param name="section"> 节点 </param>
/// <param name="key"> 键 </param>
/// <param name="value"> 要写入的新键值 </param>
/// <returns> 是否写入成功 </returns>
public bool WriteConfig( string section, string key, string value)
{
bool isWrited = false ;
try
{
if (isConfig)
{
xmlDocument.SelectSingleNode( " Configuration " ).SelectSingleNode(section).SelectSingleNode(key).InnerText = value;
xmlDocument.Save(xmlPath);
isWrited = true ;
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isWrited;
}
#region 私有方法
private bool OnXmlPathChange()
{
bool isLoaded = false ;
try
{
xmlDocument.Load(xmlPath);
isLoaded = true ;
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isLoaded;
}
#endregion
}
}
* 版权: http://XingFuStar.cnblogs.com
*
* 文件名: XmlConfig
* 文件功能描述: 简单的XML配置文件读写类
*
* 作者:XingFuStar
* 日期:2007年9月13日
*
* 当前版本:V1.0.0
*
* 修改日期:
* 修改内容:
*--------------------------------------------------------------- */
/* ----------------------------------------------------------------
* 相关的XML文件模版
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<Section1>
<Key1>Value</Key1>
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
<KeyN>Value</KeyN>
</Section1>
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
<SectionN>
<Key1>Value</Key1>
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
<KeyN>Value</KeyN>
</SectionN>
</Configuration>
*---------------------------------------------------------------- */
using System;
using System.Xml;
using System.Diagnostics;
namespace XingFuStudio.Config
{
class XmlConfig
{
private string xmlPath;
private bool isConfig;
private XmlDocument xmlDocument;
/// <summary>
/// 构造函数:装载配置文件
/// </summary>
/// <param name="xmlPath"> 配置文件的路径 </param>
public XmlConfig( string xmlPath)
{
// 请不要删除以下信息
// 版权: http://XingFuStar.cnblogs.com
this .xmlDocument = new XmlDocument();
this .XmlPath = xmlPath;
}
/// <summary>
/// Xml配置文件的路径
/// </summary>
public string XmlPath
{
set
{
xmlPath = value;
isConfig = OnXmlPathChange();
}
}
/// <summary>
/// 读取配置
/// </summary>
/// <param name="section"> 节点 </param>
/// <param name="key"> 键 </param>
/// <param name="value"> 返回的键值 </param>
/// <returns> 键值 </returns>
/// <returns> 是否读取成功 </returns>
public bool ReadConfig( string section, string key, ref string value)
{
bool isRead = false ;
try
{
if (isConfig)
{
value = xmlDocument.SelectSingleNode( " Configuration " ).SelectSingleNode(section).SelectSingleNode(key).InnerText;
isRead = true ;
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isRead;
}
/// <summary>
/// 写入配置
/// </summary>
/// <param name="section"> 节点 </param>
/// <param name="key"> 键 </param>
/// <param name="value"> 要写入的新键值 </param>
/// <returns> 是否写入成功 </returns>
public bool WriteConfig( string section, string key, string value)
{
bool isWrited = false ;
try
{
if (isConfig)
{
xmlDocument.SelectSingleNode( " Configuration " ).SelectSingleNode(section).SelectSingleNode(key).InnerText = value;
xmlDocument.Save(xmlPath);
isWrited = true ;
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isWrited;
}
#region 私有方法
private bool OnXmlPathChange()
{
bool isLoaded = false ;
try
{
xmlDocument.Load(xmlPath);
isLoaded = true ;
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isLoaded;
}
#endregion
}
}