XML文件更改插入

前端之家收集整理的这篇文章主要介绍了XML文件更改插入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. XmlDocument doc = new XmlDocument();
  2. doc.Load(xmlPaht);
  3.  
  4. XmlNodeList node = doc.SelectNodes("//Staff[Staffid='" + id + "']");
  5.  
  6. if (node.Count == 0)
  7. {
  8. //没有找到
  9. XmlNode root = doc.CreateNode(XmlNodeType.Element,"Staff",null);
  10.  
  11. XmlNode Staffid = doc.CreateNode(XmlNodeType.Element,"Staffid",null);
  12. Staffid.InnerText = id;
  13.  
  14. XmlNode LoginCount = doc.CreateNode(XmlNodeType.Element,"LoginCount",null);
  15. LoginCount.InnerText = "1";
  16.  
  17. XmlNode LastLoginTime = doc.CreateNode(XmlNodeType.Element,"LastLoginTime",null);
  18. LastLoginTime.InnerText = DateTime.Now.ToString("yyy-MM-dd hh:mm:ss");
  19.  
  20. root.AppendChild(Staffid);
  21. root.AppendChild(LoginCount);
  22. root.AppendChild(LastLoginTime);
  23.  
  24. doc.DocumentElement.AppendChild(root);
  25. }
  26. else
  27. {
  28. foreach (XmlNode Child in node)
  29. {
  30. if (Child.Name == "LoginCount")
  31. {
  32. Child.InnerText = (int.Parse(Child.InnerText) + 1).ToString();
  33. }
  34.  
  35. if (Child.Name == "LastLoginTime")
  36. {
  37. Child.InnerText = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  38. }
  39. }
  40. }
  41.  
  42. doc.Save(xmlPaht);

猜你在找的XML相关文章