xml学习(4) 创建xml 文件

前端之家收集整理的这篇文章主要介绍了xml学习(4) 创建xml 文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. /// <summary>
  2. /// 创建一个xml文件
  3. /// </summary>
  4. /// <param name="fileName">文件名字</param>
  5. private void createXmlDoc(string fileName)
  6. {
  7. string row= System.Environment.NewLine;
  8. XmlDocument xmlDoc = new XmlDocument();
  9. XmlDeclaration xde ;//表示 XML 声明节点:<?xml version='1.0'...?>
  10. xde = xmlDoc.CreateXmlDeclaration("1.0","ISO-8859-1",null);//verson:1.0 encoding:utf-8 standalone:yes(表示独立的,不依赖别的文件)
  11. xmlDoc.AppendChild(xde);
  12. XmlElement root = xmlDoc.CreateElement("BOOKS");
  13. root.SetAttribute("name","根级元素");
  14. xmlDoc.AppendChild(root);
  15.  
  16. XmlElement node1 = xmlDoc.CreateElement("BOOK");
  17. root.AppendChild(node1);
  18.  
  19. XmlElement node11 = xmlDoc.CreateElement("AUTHOR");
  20. node11.InnerText = "XMROOM";
  21. node1.AppendChild(node11);
  22.  
  23. XmlElement node12 = xmlDoc.CreateElement("NAME");
  24. node12.InnerText = "编程你最爱";
  25. node1.AppendChild(node12);
  26.  
  27. XmlElement node13 = xmlDoc.CreateElement("TIME");
  28. node13.InnerText = "2013-11-20";
  29. node1.AppendChild(node13);
  30.  
  31. XmlElement node14 = xmlDoc.CreateElement("COPYRIGHT");
  32. node14.InnerText = "XMROOM";
  33. node1.AppendChild(node14);
  34.  
  35. string path = Server.MapPath("Xml\\") + fileName + ".xml";
  36.  
  37. if (File.Exists(path))
  38. {
  39. File.Delete(path);
  40. }
  41. xmlDoc.Save(path);
  42. }

猜你在找的XML相关文章