C#读取XML配置文件的例子

发布时间:2020-10-01编辑:脚本学堂
本文介绍下,c#读取xml配置文件的实例代码,学习下c#编程操作xml文件的方法,有需要的朋友参考下。

在asp.net编程中,经常需要读取xml文件的某个指定的节点的值。

例子:
一个系统分布的部署在各个服务器上,其中一台服务器的配置文件记录了各个服务器的IP地址,系统需要集中的对各个服务器进行管理,就需要获取各个服务器的ip地址。

配置文件
 

复制代码 代码示例:
<?xml version="1.0" encoding="gb2312"?>
<iavp version="1.0">
  <config version="1">
    <db_list>
      <iavpdb>
      </iavpdb>
      <iavpdb_mgr>
     </iavpdb_mgr>
 </db_list>
    <host_list>
      <host name="localhost" ip="192.168.71.130" >
        <service name="LogServer" id="1" status="true">
          <port>12000</port>
          <bcpexcute>false</bcpexcute>
          <bcpstarttime>1</bcpstarttime>
          <bcpstoptime>3</bcpstoptime>
          <bcpinterval>4000</bcpinterval>
          <link relink_interval="900">
            <fail_dir>/logserver</fail_dir>
          </link>
          <logclient server_id="1" record_speed="100" net_error_dir="../log" reconnect_time="2">
            <log_error printscreen="true" file="false" db="false" />
            <log_information printscreen="true" file="false" db="false" />
            <log_commut printscreen="true" file="false" db="false" />
            <log_debug printscreen="true" file="false" db="false" />
          </logclient>
        </service>
 <service name="Blc" id="1" status="false">
          <logclient server_id="1" record_speed="100" net_error_dir="../log" reconnect_time="2">
            <log_error printscreen="true" file="false" db="false" />
            <log_information printscreen="true" file="false" db="false" />
            <log_commut printscreen="true" file="false" db="false" />
            <log_debug printscreen="true" file="false" db="false" />
          </logclient>
        </service>
      </host>
      <host name="localhost" ip="192.168.71.131" >
        <service name="LogServer" id="1" status="true">
          <port>12000</port>
          <bcpexcute>false</bcpexcute>
          <bcpstarttime>1</bcpstarttime>
          <bcpstoptime>3</bcpstoptime>
          <bcpinterval>4000</bcpinterval>
          <link relink_interval="900">
            <fail_dir>/logserver</fail_dir>
          </link>
          <logclient server_id="1" record_speed="100" net_error_dir="../log" reconnect_time="2">
            <log_error printscreen="true" file="false" db="false" />
            <log_information printscreen="true" file="false" db="false" />
            <log_commut printscreen="true" file="false" db="false" />
            <log_debug printscreen="true" file="false" db="false" />
          </logclient>
        </service>
 <service name="Blc" id="1" status="false">
          <logclient server_id="1" record_speed="100" net_error_dir="../log" reconnect_time="2">
            <log_error printscreen="true" file="false" db="false" />
            <log_information printscreen="true" file="false" db="false" />
            <log_commut printscreen="true" file="false" db="false" />
            <log_debug printscreen="true" file="false" db="false" />
          </logclient>
        </service>
      </host>
    </host_list>
  </config>
</iavp>

c#实现读取各个host节点的ip值。
代码:
 

复制代码 代码示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Xml;

namespace iAvpLogClear
{
    public partial class LogClear : Form
    {
        public LogClear()
        {
            InitializeComponent();
        }

        private void START_CLEAR_Click(object sender, EventArgs e)
        {
            string[] szServerAdress = new string[100];
            ReadConfig("config.xml", szServerAdress);
        }

        //读配置文件,读取配置文件中各个服务器的IP地址,存储到szServerAdress数组中
        //[in] sConfigPath: 配置文件的路径
        //[out] szServerAdress:服务器的ip地址
        public void ReadConfig( string sConfigPath, string[] szServerAdress )
        {
            XmlDocument XmlConfig = new XmlDocument();
            try
            {
                XmlConfig.Load(sConfigPath);
            }
            catch
            {
               
            }

            XmlNodeList XmlHostList;
            XmlHostList = XmlConfig.GetElementsByTagName("host");
            int i = 0;
            foreach( XmlElement host in XmlHostList )
            {
                szServerAdress[i++] = host.Attributes["ip"].Value;
            }
        }
    }