您的位置:首页 > 其它

简易读取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);

}

}

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