XmlDocument doc = new XmlDocument(); doc.Load(xmlPaht); XmlNodeList node = doc.SelectNodes("//Staff[Staffid='" + id + "']"); if (node.Count == 0) { //没有找到 XmlNode root = doc.CreateNode(XmlNodeType.Element,"Staff",null); XmlNode Staffid = doc.CreateNode(XmlNodeType.Element,"Staffid",null); Staffid.InnerText = id; XmlNode LoginCount = doc.CreateNode(XmlNodeType.Element,"LoginCount",null); LoginCount.InnerText = "1"; XmlNode LastLoginTime = doc.CreateNode(XmlNodeType.Element,"LastLoginTime",null); LastLoginTime.InnerText = DateTime.Now.ToString("yyy-MM-dd hh:mm:ss"); root.AppendChild(Staffid); root.AppendChild(LoginCount); root.AppendChild(LastLoginTime); doc.DocumentElement.AppendChild(root); } else { foreach (XmlNode Child in node) { if (Child.Name == "LoginCount") { Child.InnerText = (int.Parse(Child.InnerText) + 1).ToString(); } if (Child.Name == "LastLoginTime") { Child.InnerText = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); } } } doc.Save(xmlPaht);