C# 操作XML文件 XmlDocument和XElement

首先在根目录下新建一个config.xml:

<?xml version="1.0" encoding="utf-8"?>
<Config>
    <Debug>
        <Lan>
            <Server Ip="142.12.10.123" Port="9601"/>
        </Lan>
        <Logger enable="false" />
    </Debug>
</Config>

 

XmlDocument位于System.Xml 下,是专门处理xml节点的

XElement位于System.Xml.Linq下,是可以对xml进行linq的查询操作的

 

分别使用XmlDocument和XElement获取节点的值:

using System;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;

namespace FileXml
{
    class Program
    {
        static void Main(String[] args)
        {
            //获取xml路径
            var current_dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var xml_path = Path.Combine(current_dir, @"config.xml");

            //使用XElement快速获取节点值
            XElement xmlElement = XElement.Load(xml_path);
            String IP = xmlElement.Element("Debug").Element("Lan").Element("Server").Attribute("Ip").Value.ToString();
            Console.WriteLine(IP);
            bool isLogger = xmlElement.Element("Debug").Element("Logger").Attribute("enable").Value == "true";
            Console.WriteLine(isLogger);



            //使用XElement快速获取节点值
            XmlDocument xml_doc = new XmlDocument();
            xml_doc.Load(xml_path);
            var IP2 = xml_doc.SelectSingleNode("/Config/Debug/Lan/Server").Attributes["Ip"].Value;
            var IP2_name = xml_doc.SelectSingleNode("/Config/Debug/Lan/Server").Attributes["Ip"].Name;
            Console.WriteLine(IP2_name+":"+ IP2);
            bool isLogger2 = xml_doc.SelectSingleNode("/Config/Debug/Logger").Attributes["enable"].Value == "true";
            Console.WriteLine(isLogger2);
            Console.ReadLine();
        }
    }
}

 

总结:如果是简单查询 总体上来说两者差不多

感觉还是XmlDocument.SelectSingleNode(XPath)更方便一些

普通用XmlDocument就够了

 

 

 

 

 

Xml单例管理类:

using System;
using System.IO;
using System.Reflection;
using System.Xml;

namespace FileXml
{
    class Program
    {
        static void Main()
        {
            var ip = XmlManager.instance.XmlDoc.SelectSingleNode("/Config/Debug/Lan/Server").Attributes["Ip"].Value;
            var ip_name = XmlManager.instance.XmlDoc.SelectSingleNode("/Config/Debug/Lan/Server").Attributes["Ip"].Name;
            Console.WriteLine($"{ip_name} : {ip}");
            bool isLogger = XmlManager.instance.XmlDoc.SelectSingleNode("/Config/Debug/Logger").Attributes["enable"].Value == "true";
            Console.WriteLine(isLogger);
            Console.ReadLine();
        }
    }

    public class XmlManager
    {
        private static XmlManager _instance = null;
        public static XmlManager instance
        {
            get
            {
                if (_instance == null)
                {
                    return new XmlManager();
                }
                return _instance;
            }
        }

        private XmlDocument _xml_doc = null;
        public XmlDocument XmlDoc
        {
            get
            {
                if (_xml_doc == null)
                {
                    var current_dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                    var xml_path = Path.Combine(current_dir, @"config.xml");
                    _xml_doc = new XmlDocument();
                    _xml_doc.Load(xml_path);
                }
                return _xml_doc;
            }
        }
    }
}

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值