C# 操作XML文件 XmlDocument和XElement

C# 操作XML文件 XmlDocument和XElement首先在根目录下新建一个config.xml:<?xmlversion=”1.0″encoding=”utf-8″?><Config><Debug><Lan><ServerIp=”142.12.10.123″Port=”9601″/></Lan&g…

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

首先在根目录下新建一个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;
            }
        }
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • qt通过OpenGL实现3d游戏开发框架

    qt通过OpenGL实现3d游戏开发框架开发环境:win8编程语言c++IDE:QtCreatoropengl版本:opengles3.0(可编程渲染管线,着色器语言)OpenGLES(OpenGLforEmbeddedSystems)是OpenGL三维图形API的子集,针对手机、PDA和游戏主机等嵌入式设备而设计。该API由Khronos集团定义推广,Khronos是一个图形软硬件行业协会,…

    2022年5月25日
    34
  • 欧拉函数最全总结

    欧拉函数最全总结文章目录欧拉函数的内容一、欧拉函数的引入二、欧拉函数的定义三、欧拉函数的性质四、欧拉函数的计算方法(一)素数分解法(二)编程思维1.求n以内的所有素数2.求φ(n)3.格式化输出0-100欧拉函数表(“x?”代表十位数,“x”代表个位数)五、欧拉函数相关定理以及证明(一)定理1:缩系与欧拉函数的关系(二)定理2:缩系的充要条件(三)定理3:缩系拓展1.简单证明:(a,m)=1,(x,m)=1,故(ax,m)=1。(四)定理4:设m>1,(a,m)=1,则aφ(m)≡1(modm).1.**若ac≡bc

    2022年8月22日
    8
  • labview示波器波形采集_labview制作简易示波器教程

    labview示波器波形采集_labview制作简易示波器教程前些日子,需要保存示波器图像,可惜身边没有U盘,于是在电脑上安装了keysightBenchVue,可惜不是免费的,只能用一个月;电脑上装有Labview软件,何不自己写一个呢?下载了keysight相关示波器的编程手册,写好命令序列,可是每次打开传过来的png截屏文件,都有问题,图片软件总是显示打不开。查找了NI论坛,终于找到了问题所在,原来是截图中的一个字符需要替换。截图命令如下…

    2022年10月12日
    4
  • 优化算法——模拟退火算法

    优化算法——模拟退火算法模拟退火算法原理模拟退火算法模拟退火算法过程模拟退火算法流程模拟退火算法的Java实现Java代码最后的结果模拟退火算法原理爬山法是一种贪婪的方法,对于一个优化问题,其大致图像(图像地址)如下图所示:其目标是要找到函数的最大值,若初始化时,初始点的位置在CC处,则会寻找到附近的局部最大值AA点处,由于AA点出是一个局部最大值点,故对于爬山法来讲,该算法无法跳出局部最大值点。若初始

    2022年7月18日
    18
  • 微服务分布式事务解决方案_微服务追踪与监控

    微服务分布式事务解决方案_微服务追踪与监控目录Sleuth简介相关术语使用Sleuth引入依赖创建服务product-serviceorder-service启动&测试Zipkin使用Zipkin参考文章Sleuth简介Sleuth是SpringCloud的组件之一,它为SpringCloud实现了一种分布式追踪解决方案,兼容Zipkin,HTrace和其他基于日志的追踪…

    2025年7月7日
    2
  • 嵌入式学习视频「建议收藏」

    嵌入式学习视频「建议收藏」 http://www.verycd.com/topics/250252/

    2022年5月27日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号