前端之家收集整理的这篇文章主要介绍了
XML文件更改插入,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- 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);