Xml基础功能

XML基础功能增删改查

写作背景

想着做过的东西好会忘记 尤其是一些方法的用法 刚好有时间就把一些东西记录上去,同时进一步加深印象

XML脚本

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

namespace xmlReadWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            string root = "Root";
            string path = "test.xml";
            //创建文件测试
           //XmlFuntion.m_Install.CreateXmlDocument(path, "Root");
			//增加节点测试
            NodeInfo nodeInfo = new NodeInfo();
            nodeInfo.NodeName = "liuliang";
            nodeInfo.innetTest = "where is you name";
            nodeInfo.keyValuePairs = new Dictionary<string, string>();
            nodeInfo.keyValuePairs.Add("Name", "3");
            nodeInfo.keyValuePairs.Add("Age", "4");
            //XmlFuntion.m_Install.AddXMLNode(path, root, nodeInfo);
			//删除节点测试
            //XmlFuntion.m_Install.DeleteNode(path, "liuliang");
			//改变信息
            XmlFuntion.m_Install.ChangeNodInfo(path, nodeInfo);
        }
    }
    /// <summary>
    /// 节点信息类
    /// </summary>
    public class NodeInfo
    {
        public string NodeName;//节点名字
        public Dictionary<string, string> keyValuePairs; //属性字典
        public string innetTest;//节点文本
    }
    public class XmlFuntion
    {
        private static XmlFuntion install = null;
        public static XmlFuntion m_Install
        {
            get
            {
                if (install == null)
                {
                    install = new XmlFuntion();
                    xml = new XmlDocument();
                }
                return install;
            }

        }
        private static XmlDocument xml; //XML对象
        private FileState m_fileState = FileState.None;//文件状态
        private enum FileState
        {
            None,
            Load,
            Find,
            Save,
        }

        /// <summary>
        /// 创建Xml文档
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="RootNodeName">根节点名字</param>
        public void CreateXmlDocument(string path, string RootNodeName)
        {
            if (File.Exists(path))
            {
                Console.WriteLine("文件已存在!");
                return;
            }
            XmlDocument xmlDocument = CreateHeadFile(path, xml);
            //创建根节点
            XmlElement tmpRoot = xmlDocument.CreateElement(RootNodeName);
            xmlDocument.AppendChild(tmpRoot);
            //保存XML文档
            SaveXmlFile(path);
        }
        / <summary>
        / 创建文件
        / </summary>
        / <param name="path"></param>
        //public void CreateXmlDocument(string path)
        //{
        //    CreateHeadFile(path,xml);
        //    SaveXmlFile(path);
        //}

        /// <summary>
        /// 增加节点
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="ParentElementName"> 父节点的名字</param>
        /// <param name="newElementName">创建的名字</param>
        /// <param name="nodeInfo">增加节点信息</param>
        /// <returns></returns>
        public bool AddXMLNode(string path, string ParentElementName, NodeInfo nodeInfo)
        {
            XmlDocument xmlDocument = LoadXml(path);
            XmlElement tmpRoot = xmlDocument.DocumentElement;
            Console.WriteLine(tmpRoot.Name);
            if (!string.Equals(tmpRoot.Name, ParentElementName))
            {
                Console.WriteLine("查询不是根节点!");
            }
            XmlElement tmpParentNode = YieldNode(tmpRoot, ParentElementName);
            if (tmpParentNode == null)
            {
                Console.WriteLine("没有查询到节点");
                m_fileState = FileState.None;
                return false;
            }
            XmlElement newNode = xmlDocument.CreateElement(nodeInfo.NodeName);
            CreateNodeInfo(newNode, tmpParentNode, nodeInfo);
            SaveXmlFile(path);
            return true;

        }

        /// <summary>
        /// 按照节点名字删除
        /// </summary>
        /// <param name="path"></param>
        /// <param name=""></param>
        public void DeleteNode(string path, string deleteNodeName)
        {
            XmlDocument xmlDocument = LoadXml(path);
            XmlElement RootNode = xmlDocument.DocumentElement;
            Console.WriteLine("根节点的父节点名字 :" + RootNode.ParentNode.Name);
            XmlElement xmlDeleteNode = YieldNode(RootNode, deleteNodeName);
            if (xmlDeleteNode == null)
            {
                Console.WriteLine("没有查询到节点");
                m_fileState = FileState.None;
                return;
            }
            XmlNode xmlDeletaParentNode = xmlDeleteNode.ParentNode;
            if (xmlDeletaParentNode == null)
            {
                m_fileState = FileState.None;
                Console.WriteLine("没有找到删除节点父节点");
                return;
            }
            xmlDeletaParentNode.RemoveChild(xmlDeleteNode);
            SaveXmlFile(path);
        }
        /// <summary>
        /// 根据节点名字改变节点信息
        /// </summary>
        /// <param name="path"></param>
        /// <param name="changeNodeInfo"></param>
        public void ChangeNodInfo(string path, NodeInfo changeNodeInfo)
        {
            XmlDocument xml = LoadXml(path);
            XmlElement rootNode = xml.DocumentElement;
            XmlElement changeNode = YieldNode(rootNode, changeNodeInfo.NodeName);
            changeNode.RemoveAllAttributes();
            CreateNodeInfo(changeNode, changeNodeInfo);
            SaveXmlFile(path);
        }
        /// <summary>
        /// 加载文件
        /// </summary>
        /// <param name="Path"></param>
        /// <returns></returns>
        private XmlDocument LoadXml(string Path)
        {
            if (m_fileState != FileState.None)
            {
                Console.WriteLine("当前状态不为None,有文件被加载使用");
                return null;
            }
            m_fileState = FileState.Load;
            if (xml == null)
            {
                xml = new XmlDocument();
            }
            xml.Load(Path);
            return xml;
        }

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="path"></param>
        private void SaveXmlFile(string path)
        {
            m_fileState = FileState.Save;
            if (xml == null)
            {
                Console.WriteLine(path + "当前路径的XMLDocument为空!");
            }
            else
            {
                xml.Save(path);
            }
            m_fileState = FileState.None;
        }
        /// <summary>
        /// 创建头文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private XmlDocument CreateHeadFile(string path,XmlDocument xmlDocument)
        {
            //创建头文件
            XmlNode tmpDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", "yes");
            //给xmlDocument增加附属子项tmpDeclaration(头文件)
            xmlDocument.AppendChild(tmpDeclaration);
            return xmlDocument;
        }

        


        /// <summary>
        /// 创建节点信息
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="parentNode">父节点</param>
        /// <param name="nodeInfo">节点信息</param>
        private void CreateNodeInfo(XmlElement newNode, XmlElement parentNode, NodeInfo nodeInfo)
        {
            SetNodeInfo(newNode, nodeInfo);
            parentNode.AppendChild(newNode);
        }
        /// <summary>
        /// 设置节点信息
        /// </summary>
        /// <param name="newNode"></param>
        /// <param name="nodeInfo"></param>
        private void SetNodeInfo(XmlElement newNode, NodeInfo nodeInfo)
        {
        	//通过类来传递参数 字典保存属性集合
            if (nodeInfo.keyValuePairs.Keys.Count > 0)
            {
                foreach (var item in nodeInfo.keyValuePairs)
                {
                    newNode.SetAttribute(item.Key, item.Value);
                }
            }
            newNode.InnerText = nodeInfo.innetTest;
        }
        /// <summary>
        /// 创建节点信息
        /// </summary>
        /// <param name="newNode"></param>
        /// <param name="nodeInf"></param>
        private void CreateNodeInfo(XmlElement newNode, NodeInfo nodeInf)
        {
            SetNodeInfo(newNode, nodeInf);
        }
        /// <summary>
        /// 迭代查找节点信息
        /// </summary>
        /// <param name="RootNode"></param>
        /// <param name="FindName"></param>
        /// <returns></returns>
        private XmlElement YieldNode(XmlElement RootNode, string FindName)
        {
        //首先判断是不是根节点,其次判断根结点有没有子节点
        //在通过迭代的方式查询
            if (string.Equals(RootNode.Name, FindName))
            {
                return RootNode;
            }
            if (RootNode.ChildNodes.Count <= 0)
            {
                Console.WriteLine("跳出方法体 没有子节点");
                return null;
            }
            foreach (XmlElement item in RootNode.ChildNodes)
            {
                Console.WriteLine(item.Name);
                if (string.Equals(RootNode.Name, FindName))
                {
                    return item;
                }
                return YieldNode(item, FindName);
            }
            return null;
        }
    }
}

结尾

我想都是一些简单的代码 也就没必要仔细解释 而且还有注释 ,望观看后的大佬,可以给一些建议 谢谢

转载请附上文章链接

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值