码迷,mamicode.com
首页 > 其他好文 > 详细

XML操作

时间:2014-07-06 14:29:04      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:des   cti   for   io   new   type   

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

XML操作,布布扣,bubuko.com

XML操作

标签:des   cti   for   io   new   type   

原文地址:http://www.cnblogs.com/xiaogongzhu/p/3825620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!