zoukankan      html  css  js  c++  java
  • XML操作

     public class XMLOperation
        {
           private static readonly ILog log = LogManager.GetLogger(typeof(mainForm));
           Message mesg = null;
           XmlDocument doc;
           public XMLOperation(string fileName)
          
           {
               mesg = new Message();
               try
               {
                   doc = new XmlDocument();
                   doc.Load(fileName);
               }
               catch (Exception ex)
               {
                   Tools.LogMessage(log, mesg, ex.Message);
               }
           }
           public XMLOperation()
           {
               mesg = new Message();
               try
               {
                   doc = new XmlDocument();
               }
               catch (Exception ex)
               {
                   Tools.LogMessage(log, mesg, ex.Message);
               }
           }

           public  void CreateRootNode(string name)
           {
               try
               {
                   if (name == "" || name.Trim().Length == 0)
                   {
                       return;
                   }

                   XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                   doc.AppendChild(dec);
                   XmlNode node = doc.CreateNode(XmlNodeType.Element, name, "");
                   doc.AppendChild(node);
               }
               catch (Exception ex)
               {
                   Tools.LogMessage(log, mesg, ex.Message);
               }
           }

           public  XmlNode AddNode(string pNodeName,string nodeName)
           {
               if (nodeName == null || nodeName.Length == 0)
               {
                   return null;
               }
               XmlElement xe = null;

               try
               {
                    XmlNode nd = doc.SelectSingleNode(pNodeName);
                    xe = doc.CreateElement(nodeName);
                   nd.AppendChild(xe);
               }
               catch (Exception ex)
               {
                   Tools.LogMessage(log, mesg, ex.Message);
               }
               return xe;

           }

           public void AddElement(XmlNode pNode, string elmName,string value)
           {
               try
               {
                   XmlElement ele = doc.CreateElement(elmName);
                   ele.InnerText = value;
                   pNode.AppendChild(ele);
               }
               catch (Exception ex)
               {
                   Tools.LogMessage(log, mesg, ex.Message);
               }
           }

           public void SaveDoc(string fileName)
           {
               doc.Save(fileName);
           }

           public void ClearNodes(string rootNodeName)
           {
               try
               {
                   XmlNode root = doc.SelectSingleNode(rootNodeName);
                   if (root != null)
                   {
                       root.RemoveAll();
                   }
               }
               catch (Exception ex)
               {
                   Tools.LogMessage(log, mesg, ex.Message);
               }
           }

           public Dictionary<string, string> GetKeyValues( string rootNodeName,string elmentKeyName,string elmentKeyValue)
           {
               Dictionary<string, string> dicRet = new Dictionary<string, string>();
               try
               {
                   XmlNode rootNode = doc.SelectSingleNode(rootNodeName);
                   if (rootNode.HasChildNodes)
                   {
                       XmlNodeList nodeList = rootNode.ChildNodes;
                       foreach (XmlNode node in nodeList)
                       {
                           if (node.HasChildNodes)
                           {
                               XmlNodeList subNodeList = node.ChildNodes;
                               bool bFind = false;
                               foreach (XmlNode subNode in subNodeList)
                               {
                                   if (subNode.Name == elmentKeyName && subNode.InnerText == elmentKeyValue)
                                   {
                                       bFind = true;
                                       dicRet.Add(subNode.Name, subNode.InnerText);
                                       continue;
                                   }
                                   if (bFind)
                                   {
                                       dicRet.Add(subNode.Name, subNode.InnerText);
                                   }
                               }
                               if (bFind)
                               {
                                   break;
                               }
                           }
                       }
                   }
               }
               catch (Exception ex)
               {
                   Tools.LogMessage(log, mesg, ex.Message);
               }

               return dicRet;
           }
       
           public List<string> GetValues(string rootNodeName, string elmentKeyName)
           {
               List<string> listRet =new List<string>();
               try
               {
                   XmlNode rootNode = doc.SelectSingleNode(rootNodeName);
                   if (rootNode.HasChildNodes)
                   {
                       XmlNodeList nodeList = rootNode.ChildNodes;
                       foreach (XmlNode node in nodeList)
                       {
                           if (node.HasChildNodes)
                           {
                               XmlNodeList subNodeList = node.ChildNodes;
                               foreach (XmlNode subNode in subNodeList)
                               {
                                   if (subNode.Name == elmentKeyName)
                                   {
                                       listRet.Add(subNode.InnerText);
                                       break;
                                   }
                               }
                           }
                       }
                   }
               }
               catch (Exception ex)
               {
                   Tools.LogMessage(log, mesg, ex.Message);
               }
               return listRet;
           }

       
           public Dictionary<string, string> GetKeyValuesEx(string rootNodeName, string atrributeName)
           {
               Dictionary<string, string> dicRet = new Dictionary<string, string>();

               try
               {
                   XmlNode rootNode = doc.SelectSingleNode(rootNodeName);
                   if (rootNode.HasChildNodes)
                   {
                       XmlNodeList nodeList = rootNode.ChildNodes;
                       foreach (XmlNode node in nodeList)
                       {
                           XmlElement xe = (XmlElement)node;
                           string key = xe.GetAttribute(atrributeName);
                           if (!dicRet.ContainsKey(key))
                           {
                               dicRet.Add(key, xe.InnerText);
                           }
                       }
                   }
               }
               catch (Exception ex)
               {
                   Tools.LogMessage(log, mesg, ex.Message);
               }

               return dicRet;
           }
        
           public void RemoveNode(string rootNodeName, string keyName,string value)
           {

               XmlNode rootNode = doc.SelectSingleNode(rootNodeName);
               if (rootNode.HasChildNodes)
               {
                   XmlNodeList nodeList = rootNode.ChildNodes;
                   foreach (XmlNode node in nodeList)
                   {
                       if (node.HasChildNodes)
                       {
                           XmlNodeList subNodeList = node.ChildNodes;
                           foreach (XmlNode subNode in subNodeList)
                           {
                               if (subNode.Name == keyName)
                               {
                                   if (subNode.InnerText == value)
                                   {
                                       rootNode.RemoveChild(subNode.ParentNode);
                                       break;
                                   }
                               }
                           }
                       }
                   }
               }
           
           }
         
           public List<List<string>> GetAllKeyValues(string rootNodeName)
           {
               List<List<string>> listRet = new List<List<string>>();
               XmlNode rootNode = doc.SelectSingleNode(rootNodeName);
               if (rootNode.HasChildNodes)
               {
                   XmlNodeList nodeList = rootNode.ChildNodes;
              
                   foreach (XmlNode node in nodeList)
                   {
                     
                       if (node.HasChildNodes)
                       {
                           List<string> list = new List<string>();
                           XmlNodeList subNodeList = node.ChildNodes;
                           foreach (XmlNode subNode in subNodeList)
                           {
                               list.Add(subNode.InnerText);
                           }
                           listRet.Add(list);
                       }
                   }
               }


               return listRet;
           }
        }

  • 相关阅读:
    0929作业
    0909上机作业
    熟悉的LINUX操作
    博客搭建成功啦!
    感谢管理员,通过了我的博客邀请。哈哈
    Asp.net常用的51个代码(非常实用)
    CSS命名规范:
    常用的JavaScript验证正则表达式
    Linq to sql 查询句法
    Web.config配置文件详解
  • 原文地址:https://www.cnblogs.com/xiaogongzhu/p/3825620.html
Copyright © 2011-2022 走看看