简易读取xml文件
2008-10-16 18:16
288 查看
简易读取xml文件
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace ReadConfig
{
class ReadConfig
{
private XmlDocument xmlDoc = new XmlDocument();
private XmlNode xmlNode = null;
private string FileName = string.Empty;
private string NodePath = string.Empty;
public ReadConfig(string FileName)
{
this.FileName = FileName;
this.CreateConfig(FileName);
}
/// <summary>
/// 构造方法,创建XML流
/// </summary>
/// <param name="FileName">文件名</param>
/// <param name="NodePath">节点路径</param>
public ReadConfig(string FileName, string NodePath)
{
this.FileName = FileName;
this.NodePath = NodePath;
this.CreateConfig(FileName);
}
/// <summary>
/// 载入指定的xml文件
/// </summary>
/// <param name="p_strFileName">文件名</param>
/// <param name="p_strNodePath">指定节点</param>
private void CreateConfig(string ConfigFileName)
{
string configFile = Application.StartupPath + "//" + ConfigFileName;
if (!File.Exists(configFile))
{
MessageBox.Show("The File Was Not Found!");
}
else
{
xmlDoc.Load(configFile);
}
}
/// <summary>
/// 获取指定节点的値
/// </summary>
/// <returns>指定节点的値</returns>
public string GetNodeValue()
{
xmlNode = xmlDoc.SelectSingleNode(this.NodePath);
if (null != xmlNode)
{
return xmlNode.InnerText;
}
else
{
return "";
}
}
/// <summary>
/// 获取指定节点的値
/// </summary>
/// <returns>指定节点的値</returns>
public string GetNodeValue(string NodePath)
{
xmlNode = xmlDoc.SelectSingleNode(NodePath);
if (null != xmlNode)
{
return xmlNode.InnerText;
}
else
{
return "";
}
}
/// <summary>
/// 设置指定节点的値
/// </summary>
/// <param name="strNodeValue">修改的値</param>
public void SetNodeValue(string NodeValue)
{
xmlNode.InnerText = NodeValue;
xmlDoc.Save(this.FileName);
}
/// <summary>
/// 设置指定节点的値
/// </summary>
/// <param name="strNodeValue">修改的値</param>
public void SetNodeValue(string NodePath, string NodeValue)
{
xmlNode = xmlDoc.SelectSingleNode(NodePath);
xmlNode.InnerText = NodeValue;
xmlDoc.Save(this.FileName);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace ReadConfig
{
class ReadConfig
{
private XmlDocument xmlDoc = new XmlDocument();
private XmlNode xmlNode = null;
private string FileName = string.Empty;
private string NodePath = string.Empty;
public ReadConfig(string FileName)
{
this.FileName = FileName;
this.CreateConfig(FileName);
}
/// <summary>
/// 构造方法,创建XML流
/// </summary>
/// <param name="FileName">文件名</param>
/// <param name="NodePath">节点路径</param>
public ReadConfig(string FileName, string NodePath)
{
this.FileName = FileName;
this.NodePath = NodePath;
this.CreateConfig(FileName);
}
/// <summary>
/// 载入指定的xml文件
/// </summary>
/// <param name="p_strFileName">文件名</param>
/// <param name="p_strNodePath">指定节点</param>
private void CreateConfig(string ConfigFileName)
{
string configFile = Application.StartupPath + "//" + ConfigFileName;
if (!File.Exists(configFile))
{
MessageBox.Show("The File Was Not Found!");
}
else
{
xmlDoc.Load(configFile);
}
}
/// <summary>
/// 获取指定节点的値
/// </summary>
/// <returns>指定节点的値</returns>
public string GetNodeValue()
{
xmlNode = xmlDoc.SelectSingleNode(this.NodePath);
if (null != xmlNode)
{
return xmlNode.InnerText;
}
else
{
return "";
}
}
/// <summary>
/// 获取指定节点的値
/// </summary>
/// <returns>指定节点的値</returns>
public string GetNodeValue(string NodePath)
{
xmlNode = xmlDoc.SelectSingleNode(NodePath);
if (null != xmlNode)
{
return xmlNode.InnerText;
}
else
{
return "";
}
}
/// <summary>
/// 设置指定节点的値
/// </summary>
/// <param name="strNodeValue">修改的値</param>
public void SetNodeValue(string NodeValue)
{
xmlNode.InnerText = NodeValue;
xmlDoc.Save(this.FileName);
}
/// <summary>
/// 设置指定节点的値
/// </summary>
/// <param name="strNodeValue">修改的値</param>
public void SetNodeValue(string NodePath, string NodeValue)
{
xmlNode = xmlDoc.SelectSingleNode(NodePath);
xmlNode.InnerText = NodeValue;
xmlDoc.Save(this.FileName);
}
}
}
相关文章推荐
- Hibernate中*.hbm.xml文件让其自动读取数据库默认值
- [转载]读取WEB-INF 下applicationContext.xml配置文件
- XML文件读取
- PHP读取并输出XML文件数据的简单实现方法
- 用java程序读取xml文件中的配置信息
- xml文件读取
- KettleXMLException: 错误从XML文件读取转换 (error during reading transform from the xml file )
- js读取本地xml文件 兼容IE Firfox Chrome 等浏览器
- .net读取xml文件
- 2D游戏引擎制作:读取XML文件 4
- Jquery | ajax 读取xml 文件
- 使用c#如何读取xml文件
- flex读取外部XML文件方式
- java眼中的XML---文件读取
- Java眼中的XML--文件读取--1 应用DOM方式解析XML
- sql语句放到外部xml文件里,读取操作
- 使用ASP读取XML文件
- Java dom读取XML文件中特殊字符-报错
- ASP.NET读取XML文件4种方法分析
- EasyPlayerPro Windows播放器读取xml配置文件中的特殊字符问题