骄傲小公主

导航

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;
       }
    }

posted on 2014-07-05 12:15  骄傲小公主  阅读(438)  评论(0编辑  收藏  举报