/// <summary> /// 修改XML中的DeviceControl /// </summary> /// <param name="station"></param> /// <returns></returns> private bool EditDeviceControlFromXML(XML_DeviceControl _device) { try { XmlDocument XDoc = new XmlDocument(); XDoc.Load(Path); //加载XML文档 XmlElement ListTestEl = (XmlElement)XDoc.SelectSingleNode(@"/ClientConfig/DeviceControl"); if (ListTestEl != null) { foreach (XmlNode item in ListTestEl.ChildNodes) { XmlElement xe = (XmlElement)item; if (xe.GetAttribute("UniqueId") == _device.UniqueId.ToString()) { xe.SetAttribute("StationUniqueId", _device.StationUniqueId.ToString()); xe.SetAttribute("Number", _device.Number.ToString()); xe.SetAttribute("DeviceId", _device.DeviceId.ToString()); } } } XDoc.Save(Path); } catch (Exception ex) { LogMg.AddError(ex); return(false); } return(true); }
/// <summary> /// 保存配置到XML /// </summary> /// <returns></returns> private bool AddDeviceControlToXML(XML_DeviceControl device) { try { XmlDocument XDoc = new XmlDocument(); XDoc.Load(Path); //加载XML文档 XmlElement ListTestEl = (XmlElement)XDoc.SelectSingleNode(@"/ClientConfig/DeviceControl"); XmlElement xe = XDoc.CreateElement("Device"); xe.SetAttribute("UniqueId", device.UniqueId.ToString()); xe.SetAttribute("StationUniqueId", device.StationUniqueId.ToString()); xe.SetAttribute("Number", device.Number.ToString()); xe.SetAttribute("DeviceId", device.DeviceId.ToString()); ListTestEl.AppendChild(xe); XDoc.Save(Path); } catch (Exception ex) { LogMg.AddError(ex); return(false); } return(true); }
public bool AddDeviceControl(XML_DeviceControl device, ref string msg) { bool flag = true; try { if (AllDevice.Exists(c => c.StationUniqueId == device.StationUniqueId && c.Number == device.Number)) { msg = "该站点已存在设备编号:" + device.Number; flag = false; } else if (AllDevice.Exists(c => c.StationUniqueId == device.StationUniqueId && c.DeviceId == device.DeviceId)) { msg = "该站点已存在寄存器编号:" + device.DeviceId; flag = false; } else { lock (AllDevice) { int uniqueId = AllDevice.Max(c => c.UniqueId); device.UniqueId = uniqueId + 1; AllDevice.Add(device); flag = AddDeviceControlToXML(device); } } } catch (Exception ex) { flag = false; LogMg.AddError(ex); } return(flag); }
public List <XML_DeviceControl> GetAllDeviceControl() { List <XML_DeviceControl> list = new List <XML_DeviceControl>(); try { XmlNodeList ListNode = Utility.XmlHelper.GetXmlNodeListByXpath(Path, "/ClientConfig/DeviceControl/Device"); foreach (XmlNode item in ListNode) { XmlElement xe = (XmlElement)item; XML_DeviceControl _device = new XML_DeviceControl(); _device.UniqueId = int.Parse(xe.GetAttribute("UniqueId")); _device.StationUniqueId = int.Parse(xe.GetAttribute("StationUniqueId")); _device.Number = xe.GetAttribute("Number"); _device.DeviceId = int.Parse(xe.GetAttribute("DeviceId")); list.Add(_device); } } catch (Exception ex) { LogMg.AddError(ex); } return(list); }
/// <summary> /// 删除设备控制 /// </summary> /// <param name="uniqueId"></param> /// <param name="msg"></param> /// <returns></returns> public bool DelDeviceControl(int uniqueId, ref string msg) { XML_DeviceControl _device = AllDevice.SingleOrDefault(c => c.UniqueId == uniqueId); if (_device != null) { lock (AllDevice) { AllDevice.Remove(_device); DelCountryTestFromXML(uniqueId); return(true); } } else { msg = "不存在这个站点"; return(false); } }
/// <summary> /// 修改DeviceControl /// </summary> /// <param name="_device"></param> /// <param name="msg"></param> /// <returns></returns> public bool EditDeviceControl(XML_DeviceControl _device, ref string msg) { bool flag = true; try { lock (AllDevice) { XML_DeviceControl dc = AllDevice.SingleOrDefault(c => c.UniqueId == _device.UniqueId); AllDevice.Remove(dc); //先把这个对象删了 AllDevice.Add(_device); //再把新的对象添加进去 flag = EditDeviceControlFromXML(_device); } } catch (Exception ex) { LogMg.AddError(ex); flag = false; } return(flag); }