public NewSite(LoginEntity entity) { InitializeComponent(); if (entity != null) { PropertyInfo[] pinfo = entity.GetType().GetProperties(); foreach (PropertyInfo p in pinfo) { Control[] controllist = this.Controls.Find(p.Name, true); if (controllist != null && controllist.Count() > 0) { TextBox t = controllist[0] as TextBox; t.Text = p.GetValue(entity).ToString(); } } } }
/// <summary> /// 根据实体更新一行数据 /// </summary> /// <param name="entity"></param> /// <returns></returns> public static bool UpdateALine(LoginEntity entity) { if (entity == null) { return(false); } if (!IsExsitedSiteName(entity.SITENAME)) { return(false); } try { PropertyInfo[] pinfo = entity.GetType().GetProperties(); XmlDocument xml = new XmlDocument(); xml.Load(XmlPath); XmlNode root = xml.GetElementsByTagName("UserData")[0]; foreach (XmlNode node in root.ChildNodes) { if (node.Attributes["sitename"].Value.ToString() == entity.SITENAME) { foreach (PropertyInfo p in pinfo) { string value = ""; if (p.GetValue(entity) != null) { value = p.GetValue(entity).ToString(); } node.Attributes[p.Name.ToLower()].Value = value; } break; } } xml.Save(XmlPath); } catch { return(false); } return(true); }
/// <summary> /// 往userdata.xml文件里写入一行数据 /// </summary> /// <param name="entity">要写入的实体</param> /// <returns>成功返回true,否则返回false</returns> public static bool WriteALine(LoginEntity entity) { if (entity == null) { return(false); } if (IsExsitedSiteName(entity.SITENAME)) { return(false); } try { PropertyInfo[] pinfo = entity.GetType().GetProperties(); XmlDocument xml = new XmlDocument(); xml.Load(XmlPath); XmlNode root = xml.GetElementsByTagName("UserData")[0]; XmlElement node = xml.CreateElement("add"); foreach (PropertyInfo p in pinfo) { string value = ""; if (p.GetValue(entity) != null) { value = p.GetValue(entity).ToString(); } XmlAttribute attr = xml.CreateAttribute(p.Name.ToLower()); attr.Value = value; node.Attributes.Append(attr); } root.AppendChild(node); xml.Save(XmlPath); } catch { return(false); } return(true); }
private void Save_Click(object sender, EventArgs e) { LoginEntity entity = new LoginEntity(); PropertyInfo[] pinfo = entity.GetType().GetProperties(); foreach (PropertyInfo p in pinfo) { Control[] controllist = this.Controls.Find(p.Name, true); if (controllist != null && controllist.Count() > 0) { TextBox t = controllist[0] as TextBox; p.SetValue(entity, t.Text); } } bool issuccess = false; if (UserDataHelper.IsExsitedSiteName(entity.SITENAME)) { issuccess = UserDataHelper.UpdateALine(entity); } else { issuccess = UserDataHelper.WriteALine(entity); } if (issuccess) { MessageBox.Show("保存成功"); ScaleMain parent = (ScaleMain)this.Parent.Parent; parent.Open("Main"); this.Close(); } else { MessageBox.Show("保存失败"); } }