自定义xml文件的处理

XML实例

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <systemINFO>
    <MENUGROUPMAIN>
      <T_TMENUGROUPMAIN F_MENUGROUPCODE="1" F_MENUGROUPNAME="123"/>
      <T_TMENUGROUPMAIN F_MENUGROUPCODE="6" F_MENUGROUPNAME="666"/>
      <T_TMENUGROUPMAIN F_MENUGROUPCODE="9" F_MENUGROUPNAME="999"/>
    </MENUGROUPMAIN>
  </systemINFO>
</configuration>

//查询

 public Dictionary<int, string> GetConfigTableSetting(string tableName, string columnCode, string columnName)
        {
            string configPath;
            Dictionary<int, string> dictionary;
            XmlDocument xmlDocument;
            XmlNodeList xmlNodeList;
            XmlAttribute xmlAttrubuteCode;
            XmlAttribute xmlAttrubuteName;
            xmlDocument = new XmlDocument();
            dictionary = new Dictionary<int, string>();
            try
            {
                configPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "TMESMaintenance.exe.config";
                xmlDocument.Load(configPath);
                xmlNodeList = xmlDocument.GetElementsByTagName(tableName);
                for (int i = 0; i < xmlNodeList.Count; i++)
                {
                    xmlAttrubuteCode = xmlNodeList[i].Attributes[columnCode];
                    xmlAttrubuteName = xmlNodeList[i].Attributes[columnName];
                    dictionary.Add(Convert.ToInt32(xmlAttrubuteCode.Value), xmlAttrubuteName.Value);
                }
            }
            catch
            {
            }
            return dictionary;
        }

//添加

public bool InsertConfigSettings(string nodeName,string tableName, string columnCode,string columnName,string code,string name)
        {
            XmlDocument xmlDocument;//xml文件
            XmlElement rootNode;//根节点
            XmlNode subNode;//子节点
            XmlNode secondarySubNode;//次级子节点
            XmlElement xmlElement;//节点
            string xmlPath;//xml文件地址
            bool returnValue;
            returnValue = true;
            xmlPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "TMESMaintenance.exe.config";//获取地址
            xmlDocument = new XmlDocument();
            try
            {
                if (File.Exists(xmlPath))//xml文件是否存在
                {
                    xmlDocument.Load(xmlPath);//加载xml文件
                    rootNode = xmlDocument.DocumentElement;//获取xml根节点
                    subNode = rootNode.SelectSingleNode("systemINFO");//查询根节点中的-systemINFO
                    secondarySubNode = subNode.SelectSingleNode(nodeName);//查询节点systemINFO中的-节点(例如:MENUGROUPMAIN)
                    xmlElement = xmlDocument.CreateElement(tableName);//创建一个节点(例如:T_TMENUGROUPMAIN)
                    xmlElement.SetAttribute(columnCode, code);//添加节点属性(例如:F_MENUGROUPCODE="1")
                    xmlElement.SetAttribute(columnName, name);//添加节点属性(例如:F_MENUGROUPNAME="123")
                    secondarySubNode.AppendChild(xmlElement);//将创建的节点T_TMENUGROUPMAIN添加到MENUGROUPMAIN节点里面
                    xmlDocument.Save(xmlPath);//保存xml文件
                }
                else
                {
                    returnValue = false;
                    return returnValue;
                }
            }
            catch
            {
                returnValue = false; 
            }
            return returnValue;
        }

//修改

public bool UpdateConfigSettings(string nodeName, string tableName, string columnCode, string columnName, string name, string condition)
        {
            XmlDocument xmlDocument;//xml文件
            XmlElement rootNode;//根节点
            XmlNode subNode;//子节点
            XmlNode secondarySubNode;//次级子节点
            XmlNodeList nodes;//节点集合
            string xmlPath;//xml文件地址
            bool returnValue;
            returnValue = true;
            xmlPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "TMESMaintenance.exe.config";
            xmlDocument = new XmlDocument();
            try
            {
                if (File.Exists(xmlPath))//xml文件是否存在
                {
                    xmlDocument.Load(xmlPath);//加载xml文件
                    rootNode = xmlDocument.DocumentElement;//获取xml根节点
                    subNode = rootNode.SelectSingleNode("systemINFO");//查询根节点中的-systemINFO
                    secondarySubNode = subNode.SelectSingleNode(nodeName);//查询节点systemINFO中的-节点(例如:MENUGROUPMAIN)
                    nodes = secondarySubNode.SelectNodes(tableName);//查询MENUGROUPMAIN中的所有节点
                    foreach (XmlNode node in nodes)//循环节点集合
                    {
                        if (node.Attributes[columnCode].Value == condition)//判断要修的属性
                        {
                            node.Attributes[columnName].Value = name;//修改属性
                        }
                    }
                    xmlDocument.Save(xmlPath);//保存xml文件
                }
                else
                {
                    returnValue = false;
                    return returnValue;
                }
            }
            catch
            {
                returnValue = false;
            }
            return returnValue;
        }

//删除

public bool DeleteConfigSettings(string nodeName, string tableName, string columnCode, string condition)
        {
            XmlDocument xmlDocument;//xml文件
            XmlElement rootNode;//根节点
            XmlNode subNode;//子节点
            XmlNode secondarySubNode;//次级子节点
            XmlNodeList nodes;//节点集合
            string xmlPath;//xml文件地址
            bool returnValue;
            returnValue = true;
            xmlPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "TMESMaintenance.exe.config";
            xmlDocument = new XmlDocument();
            try
            {
                if (File.Exists(xmlPath))//xml文件是否存在
                {
                    xmlDocument.Load(xmlPath);//加载xml文件
                    rootNode = xmlDocument.DocumentElement;//获取xml根节点
                    subNode = rootNode.SelectSingleNode("systemINFO");//查询根节点中的-systemINFO
                    secondarySubNode = subNode.SelectSingleNode(nodeName);//查询节点systemINFO中的-节点(例如:MENUGROUPMAIN)
                    nodes = secondarySubNode.SelectNodes(tableName);//查询MENUGROUPMAIN中的所有节点
                    for (int i = 0; i < nodes.Count; i++) //循环节点集合
                    {
                        if (nodes[i].Attributes[columnCode].Value == condition) //判断要删除的属性
                        {
                            nodes[i].ParentNode.RemoveChild(nodes[i]);//删除节点
                        }
                    }
                    xmlDocument.Save(xmlPath);//保存xml文件
                }
                else
                {
                    returnValue = false;
                    return returnValue;
                }
            }
            catch
            {
                returnValue = false;
            }
            return returnValue;
        }

转载于:https://www.cnblogs.com/zcr1829991218/p/ElevenNights_CSharp_XMLOperation.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值