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

  • 相关阅读:
    EasyNVR RTSP转RTMPHLS流媒体服务器前端构建之:使用BootstrapPagination以分页形式展示数据信息
    EasyNVR H5无插件直播方案前端构建之:videojs初始化的一些样式处理
    EasyNVR H5无插件直播方案前端构建之:如何播放HLS
    EasyNVR RTSP转RTMP/HLS流媒体服务器前端构建之:bootstrap弹窗功能的实现
    EasyNVR H5无插件直播方案前端构建之:如何区分PC端和移动端
    EasyNVR RTSP转RTMPHLS流媒体服务器前端构建之:通过接口获取实时信息
    实现RTSP摄像机进行网页直播和微信直播的技术方案
    EasyNVR RTSP转RTMPHLS流媒体服务器前端构建之:bootstrapdatepicker日历插件的实时动态展现
    EasyNVR H5无插件直播方案前端构建之:播放界面添加实时云台控制界面
    EasyNVR H5无插件直播方案前端构建之:实时直播的四分屏的前端展示
  • 原文地址:https://www.cnblogs.com/xiaogongzhu/p/3825620.html
Copyright © 2020-2023  润新知