简易读取xml文件

简易读取xml文件

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Xml;
  7. namespace ReadConfig
  8. {
  9.     class ReadConfig
  10.     {
  11.         private XmlDocument xmlDoc = new XmlDocument();
  12.         private XmlNode xmlNode = null;
  13.         private string FileName = string.Empty;
  14.         private string NodePath = string.Empty;
  15.         public ReadConfig(string FileName)
  16.         {
  17.             this.FileName = FileName;
  18.             this.CreateConfig(FileName);
  19.         }
  20.         /// <summary>
  21.         /// 构造方法,创建XML流
  22.         /// </summary>
  23.         /// <param name="FileName">文件名</param>
  24.         /// <param name="NodePath">节点路径</param>
  25.         public ReadConfig(string FileName, string NodePath)
  26.         {
  27.             this.FileName = FileName;
  28.             this.NodePath = NodePath;
  29.             this.CreateConfig(FileName);
  30.         }
  31.         /// <summary>
  32.         /// 载入指定的xml文件
  33.         /// </summary>
  34.         /// <param name="p_strFileName">文件名</param>
  35.         /// <param name="p_strNodePath">指定节点</param>
  36.         private void CreateConfig(string ConfigFileName)
  37.         {
  38.             string configFile = Application.StartupPath + "//" + ConfigFileName;
  39.             if (!File.Exists(configFile))
  40.             {
  41.                 MessageBox.Show("The File Was Not Found!");
  42.             }
  43.             else
  44.             {
  45.                 xmlDoc.Load(configFile);
  46.             }
  47.         }
  48.         /// <summary>
  49.         /// 获取指定节点的値
  50.         /// </summary>
  51.         /// <returns>指定节点的値</returns>
  52.         public string GetNodeValue()
  53.         {
  54.             xmlNode = xmlDoc.SelectSingleNode(this.NodePath);
  55.             if (null != xmlNode)
  56.             {
  57.                 return xmlNode.InnerText;
  58.             }
  59.             else
  60.             {
  61.                 return "";
  62.             }
  63.         }
  64.         /// <summary>
  65.         /// 获取指定节点的値
  66.         /// </summary>
  67.         /// <returns>指定节点的値</returns>
  68.         public string GetNodeValue(string NodePath)
  69.         {
  70.             xmlNode = xmlDoc.SelectSingleNode(NodePath);
  71.             if (null != xmlNode)
  72.             {
  73.                 return xmlNode.InnerText;
  74.             }
  75.             else
  76.             {
  77.                 return "";
  78.             }
  79.         }
  80.         /// <summary>
  81.         /// 设置指定节点的値
  82.         /// </summary>
  83.         /// <param name="strNodeValue">修改的値</param>
  84.         public void SetNodeValue(string NodeValue)
  85.         {
  86.             xmlNode.InnerText = NodeValue;
  87.             xmlDoc.Save(this.FileName);
  88.         }
  89.         /// <summary>
  90.         /// 设置指定节点的値
  91.         /// </summary>
  92.         /// <param name="strNodeValue">修改的値</param>
  93.         public void SetNodeValue(string NodePath, string NodeValue)
  94.         {
  95.             xmlNode = xmlDoc.SelectSingleNode(NodePath);
  96.             xmlNode.InnerText = NodeValue;
  97.             xmlDoc.Save(this.FileName);
  98.         }
  99.     }
  100. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值