示例#1
0
        /// <summary>
        /// 根据AP服务器的名称获取AP监控设置
        /// </summary>
        /// <param name="apName">AP服务器名称</param>
        /// <returns>AP服务器的AP监控设置</returns>
        public APInfo GetApInfo(string apName)
        {
            APInfo apInfo = null;

            try
            {
                //获取要操作文件的路径
                string fileInfo = System.Windows.Forms.Application.StartupPath + "\\" + AppConst.directoryAppConfig + "\\" + AppConst.APConfig + ".xml";
                //判断文件是否存在
                if (!File.Exists(fileInfo))
                {
                    return(null);
                }

                XmlDocument xDoc = new XmlDocument();
                //获取XML内容
                xDoc.Load(fileInfo);
                XmlNodeList xmlnList = xDoc.SelectNodes("//root//" + elementName);

                foreach (XmlNode node in xmlnList)
                {
                    if (node.Attributes["APName"].Value.ToUpper() == apName.ToUpper())
                    {
                        apInfo                  = new APInfo();
                        apInfo.ApName           = node.Attributes["APName"].Value;
                        apInfo.ApAddress        = node.Attributes["APAddress"].Value;
                        apInfo.AlarmTime        = node.Attributes["AlarmTime"].Value;
                        apInfo.AlarmStatus      = node.Attributes["AlarmStatus"].Value;
                        apInfo.LastFileLocation = node.Attributes["LastFileLocation"].Value;
                        apInfo.LastCheckTime    = node.Attributes["LastCheckTime"].Value;
                        break;
                    }
                }

                xDoc = null;
            }
            catch
            {
                return(null);
            }

            return(apInfo);
        }
示例#2
0
        /// <summary>
        /// 需要保存的信息:AP名称、路径、报警时间、是否报警、上次检测文件的位置、上次检测时间等
        /// </summary>
        /// <param name="apInfo">AP监控设置信息</param>
        /// <returns>保存结果</returns>
        public bool SaveAPNote(APInfo apInfo)
        {
            try
            {
                //获取要操作文件的路径
                string fileInfo = System.Windows.Forms.Application.StartupPath + "\\" + AppConst.directoryAppConfig + "\\" + AppConst.APConfig + ".xml";
                //判断文件是否存在
                if (!File.Exists(fileInfo))
                {
                    //创建文件
                    CreateXml(fileInfo);
                }

                XmlDocument xDoc = new XmlDocument();
                //获取XML内容
                xDoc.Load(fileInfo);
                XmlNodeList xmlnList = xDoc.SelectNodes("//root//" + elementName);
                XmlNode     rootNode = xDoc.SelectSingleNode("//root");

                int rowIndex = 0;
                int maxIndex = 0;
                //移除已经存在的节点
                foreach (XmlNode node in xmlnList)
                {
                    if (node.Attributes["APName"].Value.ToUpper() == apInfo.ApName.ToUpper())
                    {
                        //存在该节点,则取保存的上次文件位置和上次文件检测时间
                        if (string.IsNullOrEmpty(apInfo.LastFileLocation))
                        {
                            apInfo.LastFileLocation = node.Attributes["LastFileLocation"].Value;
                            apInfo.LastCheckTime    = node.Attributes["LastCheckTime"].Value;
                        }
                        rowIndex = int.Parse(node.Attributes["RowIndex"].Value);

                        //移除该节点
                        rootNode.RemoveChild(node);
                        break;
                    }
                    else
                    {
                        int index = int.Parse(node.Attributes["RowIndex"].Value);
                        if (index > maxIndex)
                        {
                            maxIndex = index;
                        }
                    }
                }

                if (rowIndex == 0)
                {
                    rowIndex = maxIndex + 1;
                }

                #region 保存节点
                XmlNode newNode = xDoc.CreateElement(elementName);

                XmlNode xmlMAttribute1 = xDoc.CreateNode(XmlNodeType.Attribute, "APName", "");
                xmlMAttribute1.Value = apInfo.ApName;
                newNode.Attributes.SetNamedItem(xmlMAttribute1);
                XmlNode xmlMAttribute2 = xDoc.CreateNode(XmlNodeType.Attribute, "APAddress", "");
                xmlMAttribute2.Value = apInfo.ApAddress;
                newNode.Attributes.SetNamedItem(xmlMAttribute2);
                XmlNode xmlMAttribute3 = xDoc.CreateNode(XmlNodeType.Attribute, "AlarmTime", "");
                xmlMAttribute3.Value = apInfo.AlarmTime;
                newNode.Attributes.SetNamedItem(xmlMAttribute3);
                XmlNode xmlMAttribute4 = xDoc.CreateNode(XmlNodeType.Attribute, "AlarmStatus", "");
                xmlMAttribute4.Value = apInfo.AlarmStatus;
                newNode.Attributes.SetNamedItem(xmlMAttribute4);
                XmlNode xmlMAttribute5 = xDoc.CreateNode(XmlNodeType.Attribute, "LastFileLocation", "");
                xmlMAttribute5.Value = apInfo.LastFileLocation;
                newNode.Attributes.SetNamedItem(xmlMAttribute5);
                XmlNode xmlMAttribute6 = xDoc.CreateNode(XmlNodeType.Attribute, "LastCheckTime", "");
                xmlMAttribute6.Value = apInfo.LastCheckTime;
                newNode.Attributes.SetNamedItem(xmlMAttribute6);
                XmlNode xmlMAttribute7 = xDoc.CreateNode(XmlNodeType.Attribute, "RowIndex", "");
                xmlMAttribute7.Value = rowIndex.ToString();
                newNode.Attributes.SetNamedItem(xmlMAttribute7);


                rootNode.AppendChild(newNode);
                #endregion

                //保存文件
                xDoc.Save(fileInfo);
                xDoc = null;
            }
            catch
            {
                return(false);
            }

            return(true);
        }