首页 > Windows开发 > 详细

C#读取xml文件静态类

时间:2018-01-14 11:35:53      阅读:235      评论:0      收藏:0      [点我收藏+]
    internal static class XmlUtil
    {
        internal static XmlDocument doc;
        internal static string path;

        //在以下情况下执行该函数:1).当class不为static,实例化class时;2).当调用静态字段、方法、属性等时
        //但该函数仅执行一次
        static XmlUtil()
        {
            path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            path = Path.Combine(path, "Config.xml");
            doc = new XmlDocument();
            doc.Load(path);
        }
        
        internal static void SetValue(string valueName, string value)
        {
            XmlNode node = doc.DocumentElement.SelectSingleNode(valueName);
            node.InnerText = value;
            doc.Save(path);
        }

        internal static string GetValue(string valueName)
        {
            XmlNode node = doc.DocumentElement.SelectSingleNode(valueName);
            string result = node.InnerText;
            return result;
        }

    }

用法:

        static void Main(string[] args)
        {
            string v1 = XmlUtil.GetValue(@"/config/A2/AA1");
            Console.WriteLine(v1);

            v1 = XmlUtil.GetValue(@"A2/AA1");
            Console.WriteLine(v1);

            Console.ReadLine();
        }

 

C#读取xml文件静态类

原文:https://www.cnblogs.com/x2009/p/8282471.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!