zoukankan      html  css  js  c++  java
  • C# 读写XML文件示例

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;
    using System.Xml;
    using System.Web;
    
    namespace MSCL
    {
        /// <summary>
        /// 配置文件帮助类
        /// </summary>
        public class ConfigExtHelper
        {
            /*
                <?xml version="1.0" encoding="utf-8" ?>
                <config>
                  <token>king</token>
                  <cookie>123456</cookie>
                  <pwd>123456</pwd>
                </config>
             */
            /// <summary>
            /// xml文件路径
            /// </summary>
            public static string filepath =  System.Web.HttpContext.Current.Server.MapPath("/XML/config.xml");
    
            /// <summary>
            /// 设置值
            /// </summary>
            /// <param name="key">key</param>
            /// <param name="value">value</param>
            public static void Set(string key, string value)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(filepath);
                XmlNode node;
                node = xmlDoc.SelectSingleNode("config/" + key);
                if (node == null)
                {
                    XmlElement n = xmlDoc.CreateElement(key);
                    n.InnerText = value;
                    xmlDoc.SelectSingleNode("config").AppendChild(n);
                }
                else
                {
                    node.InnerText = value;
                }
                xmlDoc.Save(filepath);
            }
    
            /// <summary>
            /// 根据key获取value
            /// </summary>
            /// <param name="key">key</param>
            /// <returns></returns>
            public static string Get(string key)
            {
                //获得数据
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(filepath);
                XmlNode node;
                node = xmlDoc.SelectSingleNode("config/" + key);
                string str = node == null ? "" : node.InnerText;
                return str;
            }
        }
    }
  • 相关阅读:
    Unity3D-光照贴图技术
    登岳麓山
    第一个OC程序
    Unity3D之碰撞体,刚体
    TypeError: 'stepUp' called on an object that does not implement interface HTMLInputElement
    QQ互联登录回调路径错误redirect uri is illegal(100010)
    Quartz.Net使用
    C# 文件相关操作
    微信扫码支付模式一和模式二的区别
    ankhSVN安装后,VS2010使用
  • 原文地址:https://www.cnblogs.com/smartsmile/p/7666238.html
Copyright © 2011-2022 走看看