C#读取XML的方法

本文介绍如何使用C#语言来读取XML文件,通过示例代码详细展示了读取XML的基本步骤。
摘要由CSDN通过智能技术生成

C#读取XML文件的方法
直接上代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Diagnostics;

namespace ConsoleApp14
{
    class Program
    {
        static void Main(string[] args)
        {

            getValue("system", "srcPath");

        }

        /// <summary>
        /// 获取XML配置文件的值
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string getValue(string section, string key)
        {
            string retValue = string.Empty;
            XmlDocument xd = new XmlDocument();
            xd.Load("C:\\Users\\Administrator\\source\\repos\\ConsoleApp14\\ConsoleApp14\\bin\\Debug\\configlFile.xml");
            XmlElement xe = xd.DocumentElement;
            if (xe.Name == "configuration")
            {
                //根据固定的文档结构查找指定配置项
                foreach (System.Xml.XmlLinkedNode xcea in xe.ChildNodes)
                {
                    if (xcea.NodeType != System.Xml.XmlNodeType.Element)  //如果不是元素,继续
                    {
                        continue;
                    }
                    System.Xml.XmlElement xce = (System.Xml.XmlElement)xcea;
                    if ((xce.NodeType == System.Xml.XmlNodeType.Element) && (xce.HasAttributes) && (xce.Name == "Section"))
                    {
                        if (xce.Attributes[0].Name == "Name" && xce.Attributes[0].Value == section)  // 通过属性判断当前元素是否是需要查找的配置项
                        {
                            foreach (System.Xml.XmlLinkedNode xccea in xce.ChildNodes)
                            {
                                if (xccea.NodeType != System.Xml.XmlNodeType.Element)  //如果不是元素,继续
                                {
                                    continue;
                                }

                                System.Xml.XmlElement xcce = (System.Xml.XmlElement)xccea;
                                if (xcce.NodeType == System.Xml.XmlNodeType.Element && xcce.HasAttributes && xcce.Name == "Key")
                                {
                                    if (xcce.Attributes[0].Name == "Name" && xcce.Attributes[0].Value == key)
                                    {
                                        if (string.IsNullOrEmpty(retValue))
                                        {
                                            retValue = xcce.Attributes[1].Value;
                                        }
                                        else
                                        {
                                            retValue = retValue + "," + xcce.Attributes[1].Value;
                                        }

                                    }
                                }
                            }
                        }
                    }
                }
            }
            return retValue;
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值