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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Web后端开发入门(1)

    Web概述:    在英语中web表示网页的意思,它用于表示Internet主机上供外界访问的资源。Internet上供外界访问的Web资源分为:    1.静态Web资源(如html页面):指web页面中始终不变的数据。    2.动态Web资源:指web页面中供人们浏览的数据是由程序产生的,也就是实时变化的数据。动态Web应用的实现手段:    静态web开发技术有html,常用动态web开发…

    2022年4月4日
    42
  • 二叉树计算叶子节点的计算问题

    二叉树计算叶子节点的计算问题题目如下:计算方法需要使用两个结论:1:总结点数=树中的边数+1。见下图可以清晰明白。2:叶子节点数=总结点数-度数非零的节点数(戒子节点度为0)所以本题目解法:总结点数=20*4+10

    2022年8月1日
    8
  • BindService详解

    BindService详解Service启动得两种方式分别为startService()、bindService(),但是他们的使用场景是不一样的。。。

    2022年6月10日
    40
  • jenkins allure_jenkins api 文档

    jenkins allure_jenkins api 文档前言jenkins集成了allure插件,安装插件后运行pytest+allure的脚本即可在jenkins上查看allure报告了。allure安装在运行代码的服务器本机,我这里是用的dock

    2022年7月28日
    8
  • agrs php zf2_AGRS币是什么东西,投资理财区块链数字货币

    agrs php zf2_AGRS币是什么东西,投资理财区块链数字货币AGRS币是什么东西,投资理财区块链数字货币如何高回报投zi数字货币,最后一波10~100倍财富回报盛宴,错过了就不再有!大家好,七年虚拟货币玩家,大牛谈不上,但是也带着上万名学生做了几年投zi了,没错过比特币,没错过以太坊,没错过AGRS币,回报在1000倍左右了从2013年年前开始带学生投zi区块链,学生的平均回报也在200倍左;欢迎大家来一起交流,互相学习!如何在AGRS币牛市和熊市的切换中…

    2022年5月30日
    33
  • python django做网页_响应式网页怎么做啊

    python django做网页_响应式网页怎么做啊这篇文字适合刚学习Django的同学,如果比较熟的就不用看了。以下都是讲在windows上的部署情况;准备:1、python3.62、pycharm profession(专业版)3、安装Django模块以上的安装就不讲了,比较简单,网上也有很多教程。都去官网下载安装即可。 前言:学习django框架其实就是学习它的文件目录,目录下有一些必须的模块和包,当然你也…

    2022年10月13日
    4

发表回复

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

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