private bool EditStationFromXML(XML_Station station) { try { XmlDocument XDoc = new XmlDocument(); XDoc.Load(Path); //加载XML文档 XmlElement ListTestEl = (XmlElement)XDoc.SelectSingleNode(@"/ClientConfig/Stations"); if (ListTestEl != null) { foreach (XmlNode item in ListTestEl.ChildNodes) { XmlElement xe = (XmlElement)item; if (xe.GetAttribute("UniqueId") == station.UniqueId.ToString()) { xe.SetAttribute("TransferCode", station.TransferCode); xe.SetAttribute("StationId", station.StationId.ToString()); xe.SetAttribute("OrgId", station.OrgId.ToString()); xe.SetAttribute("站点名称", station.StationName); } } } XDoc.Save(Path); } catch (Exception ex) { LogMg.AddError(ex); return(false); } return(true); }
/// <summary> /// 添加客户端连接 (注册客户端) /// </summary> /// <param name="tel">电话号码</param> /// <param name="socket">socket实例</param> /// <param name="ip">客户端DTU的IP</param> /// <param name="port">客户端DTU端口</param> public static void AddClient(string tel, Socket socket) { LogMg.AddDebug(string.Format("进入AddClient方法 tel:{0}", tel)); Clazz.DTUClientInfo clientInfo = Clients.SingleOrDefault(c => c.TelOrGprsId == tel); XML_Station station = SysConfig.DTU_StationConfig.Stations.SingleOrDefault(c => c.Tel == tel); lock (Clients) { if (clientInfo == null) //如果客户端不存在,则添加,如果存在,则修改。 { clientInfo = new Clazz.DTUClientInfo(); clientInfo.TelOrGprsId = tel; //手机号码 clientInfo.socket = socket; //客户端连接实例 clientInfo.RegisterTime = DateTime.Now; //注册时间 clientInfo.LastVisitTime = DateTime.Now; //最后一次访问时间 if (station != null) { clientInfo.StationId = station.StationId; //站点id clientInfo.Protocol = station.Protocol; } Clients.Add(clientInfo); } else { clientInfo.socket = socket; clientInfo.LastVisitTime = DateTime.Now; //最后一次访问时间 } if (ClientChangeHander != null) { ClientChangeHander(); } } LogMg.AddDebug(string.Format("退出AddClient方法 tel:{0}", tel)); }
/// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_Save_Click(object sender, EventArgs e) { //新增 try { if (!Validation()) { return; } XML_Station _station = new XML_Station(); _station.StationId = int.Parse(txt_stationId.Text); _station.StationName = txt_stationName.Text; _station.OrgId = txt_orgid.Text; _station.TransferCode = txt_transferCode.Text; string msg = ""; if (UniqueId == 0) { if (SysConfig.clientConfig.AddStation(_station, ref msg)) { MessageBox.Show("添加成功"); this.Close(); } else { MessageBox.Show(msg); } } else { //修改 _station.UniqueId = UniqueId; if (SysConfig.clientConfig.EditStation(_station, ref msg)) { MessageBox.Show("修改成功"); this.Close(); } else { MessageBox.Show(msg); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); LogMg.AddError(ex); } }
public bool EditStation(XML_Station station, ref string msg) { bool flag = true; try { lock (AllStation) { XML_Station s = AllStation.SingleOrDefault(c => c.UniqueId == station.UniqueId); AllStation.Remove(s); //先把这个对象删了 AllStation.Add(station); //再把新的对象添加进去 flag = EditStationFromXML(station); } } catch (Exception ex) { LogMg.AddError(ex); flag = false; } return(flag); }