C#读取xml文件静态类

    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();
        }

 

转载于:https://www.cnblogs.com/x2009/p/8282471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值