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


相关推荐

  • windows中bat批处理的注释语句

    windows中bat批处理的注释语句转自:wh_19910525https://blog.csdn.net/wh_19910525/article/details/8125762写bat批处理也一样,都要用到注释的功能,这是为了程式的可读性在批处理中,段注释有一种比较常用的方法:gotostart=可以是多行文本,可以是命令=可以包含重定向符号和其他特殊字符=只要不包含:start这一行,就都是…

    2025年6月3日
    2
  • fileinput.js php,fileinput

    fileinput.js php,fileinput$(“#uploadfile”).fileinput({theme:’explorer-fa’,uploadUrl:upload_url+”?catalog=9″,language:’zh’,overwriteInitial:false,initialPreviewAsData:true,maxFileCount:3,initialPreview:pics,initialPrev…

    2022年6月7日
    28
  • 数字 和 大小写字母之间的转换 10进制和26进制之间的转换「建议收藏」

    数字 和 大小写字母之间的转换 10进制和26进制之间的转换「建议收藏」/**数字转大写字母(26进制)1-&gt;A2-&gt;B*@sinceJDK1.8*/publicstaticStringnumCovertLetter(intnum){if(num&lt;=0){thrownewRuntimeException("参数必须大于0");…

    2022年9月2日
    3
  • request对象的作用

    request对象的作用HttpServletRequset:请求报文     代表:浏览器请求时的请求报文,请求到达服务器服务器将报文解析封装为这个对象     获取:请求到服务器是,服务器直接创建然后传入到servlet方法中,最终传入到doget中     作用:获取请求报文中的所有数据              1、获取请求参数【input表…

    2022年5月29日
    43
  • 写一个函数,获取一篇文章内容中的全部图片,并下载

    写一个函数,获取一篇文章内容中的全部图片,并下载

    2021年11月4日
    43
  • linux ftp下载命令_centos如何连接ftp

    linux ftp下载命令_centos如何连接ftpLinuxFTP命令全集1前言下面就所有命令给出解释和例子。说明: 1.remote-file指远程文件,即服务器上的文件2.local-file 指本地文件,即本地机器上的文件2登录登出命令2.1ftp$ftp192.168.0.2格式:ftp[host]Host为ftp主机ip,此命令用来登录ftp服务器,登录后会提示输入账户和密码,账户和密码正确输入后,就会登录到…

    2022年9月21日
    3

发表回复

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

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