Common/XMLHelper.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Common
{
    public class XMLHelper
    {
        /// <summary>
        /// 读取XML
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Dictionary<string, string> ReadXML(string path="setting.xml")
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            //检查是否存在配置文件
            if (!File.Exists(path))
            {
                dic = GetDefaultSetting();
                var isSaveSuccess=  WriteXML(dic);
                if (!isSaveSuccess)
                {
                    return null;
                }
            }
            try
            {
                dic = new Dictionary<string, string>();
                XDocument document = XDocument.Load(path);
                //获取到XML的根元素进行操作
                XElement root = document.Root;

                //获取根元素下的所有子元素
                IEnumerable<XElement> enumerable = root.Elements();
                foreach (XElement item in enumerable)
                {
                    XElement Xmlname = item.Element("name");
                    XElement Xmlvalue = item.Element("value");
                    if (Xmlname != null && Xmlvalue != null)
                    {
                        dic.Add(Xmlname.Value, Xmlvalue.Value);
                    }


                }
            }
            catch 
            {
                ;
            }
           
            //配置文件被人修改
            if (dic.Count!=7)
            {
                return GetDefaultSetting();
            }
后面还有 58 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。