C# 操作XML文件 XmlDocument和XElement

2022-09-06 10:27:40 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

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

代码语言:javascript复制
<?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获取节点的值:

代码语言:javascript复制
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单例管理类:

代码语言:javascript复制
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;
            }
        }
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/150733.html原文链接:https://javaforall.cn

0 人点赞