C#操作XML文件(自用)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Web.Model.DBModel;

namespace Web.Helper
{
    public class XmlHelper
    {
        public static readonly string XmlPath = System.Web.HttpContext.Current.Server.MapPath("/Config/CustomGroupListJXML.xml");

        /// <summary>
        /// 查询所有的Text
        /// </summary>
        /// <returns></returns>
        public List<string> SelectAllText()
        {
            List<string> list = new List<string>();
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(XmlPath);
            XmlNode xn = xmlDocument.SelectSingleNode("CustomGroupList");
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xn1 in xnl)
            {
                XmlElement xe = (XmlElement)xn1;
                if (xe.GetAttribute("Text")!="系统默认")
                {
                    list.Add(xe.GetAttribute("Text"));
                }
            }
            return list;

        }

        /// <summary>
        /// 根据节点的text来读取所有子节点
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public Dictionary<string,string> ReadXmlByText(string text=null)
        {
            try
            {
                Dictionary<string, string> returnDic = new Dictionary<string, string>();
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(XmlPath);
                XmlNode xn = xmlDocument.SelectSingleNode("CustomGroupList");
                XmlNodeList xnl = xn.ChildNodes;
                List<string> list = new List<string>();
                foreach (XmlNode xn1 in xnl)
                {
                    XmlElement xe = (XmlElement)xn1;
                    if (!string.IsNullOrEmpty(text))
                    {
                        if (xe.GetAttribute("Text").ToString().IndexOf(text) > -1)
                        {
                            XmlNodeList xnl0 = xe.ChildNodes;
                            for (int i = 0; i < xnl0.Count; i++)
                            {
                                returnDic.Add(xnl0.Item(i).Name, xnl0.Item(i).InnerText);
                            }
                        }
                    }
                }
                return returnDic;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 增加节点
        /// </summary>
        /// <param name="nodeName"></param>
        /// <param name="childNode"></param>
        /// <returns></returns>
        public string AddXmlNode(string nodeName, Dictionary<string, string> childNode)
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(XmlPath);
                XmlNode root = xmlDocument.SelectSingleNode("CustomGroupList");
                XmlElement xelKey = xmlDocument.CreateElement("AllFields");
                XmlAttribute xelType = xmlDocument.CreateAttribute("Text");
                xelType.InnerText = nodeName;
                xelKey.SetAttributeNode(xelType);
                foreach (var item in childNode)
                {
                    XmlElement xel = xmlDocument.CreateElement(item.Key);
                    xel.InnerText = item.Value;
                    xelKey.AppendChild(xel);
                    root.AppendChild(xelKey);
                }
                xmlDocument.Save(XmlPath);
                return "success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public string DeleteXmlNode(string text = null)
        {
            try
            {
                bool bl = false;
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(XmlPath);
                XmlNode root = xmlDocument.SelectSingleNode("CustomGroupList");
                XmlNodeList child = root.ChildNodes;
                foreach (XmlNode xn1 in child)
                {
                    XmlElement xe = (XmlElement)xn1;
                    if (xe.GetAttribute("Text").IndexOf(text.Trim()) > -1)
                    {
                        root.RemoveChild(xn1);
                        bl = true;
                    }
                }
                xmlDocument.Save(XmlPath);
                if (bl)
                {
                    return "success";
                }
                else {
                    return "XML里找不到节点("+ text + ")";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }
        /// <summary>
        /// 增加或移除子节点
        /// </summary>
        /// <param name="text"></param>
        /// <param name="childNode"></param>
        /// <returns></returns>
        public string UpdateXmlNodes(string text, Dictionary<string, string> childNode,bool IsDelete)
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(XmlPath);
                XmlNode root = xmlDocument.SelectSingleNode("CustomGroupList");
                XmlNodeList child = root.ChildNodes;
                foreach (XmlNode xn1 in child)
                {
                    XmlElement xe = (XmlElement)xn1;
                    if (xe.GetAttribute("Text").IndexOf(text.Trim()) > -1)
                    {
                        XmlNodeList child1 = xn1.ChildNodes;
                        if (IsDelete)
                        {
                            for (int i = 0; i < child1.Count; i++)
                            {
                                if (childNode.ContainsValue(child1.Item(i).InnerText))
                                {
                                    xn1.RemoveChild(child1.Item(i));
                                    i = 0;
                                }
                            }
                        }
                        else
                        {
                            foreach (var item in childNode)
                            {
                                XmlElement xel = xmlDocument.CreateElement(item.Key);
                                xel.InnerText = item.Value;
                                xe.AppendChild(xel);
                            }
                        }
                    }
                }
                xmlDocument.Save(XmlPath);
                return "success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值