XML文件系列二之XML基本操作

前端之家收集整理的这篇文章主要介绍了XML文件系列二之XML基本操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

XML文件是一种以简单文本格式存储数据的方式。下面介绍XML文件的几中基本操作。
1、新建XML文件

  1. /// <summary>
  2. /// 1.新建XML文件
  3. /// </summary>
  4. public static void CreateXML()
  5. {
  6. XmlDocument doc = new XmlDocument();
  7. //xml declaration (xml声明)
  8. XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0","utf-8",null);
  9. XmlNode rootNode = doc.CreateNode(XmlNodeType.Element,"v","Games","www-microsoft-game");
  10. doc.AppendChild(rootNode);
  11. XmlNode node1 = doc.CreateNode(XmlNodeType.Element,"Game","www-microsoft-game");
  12. rootNode.AppendChild(node1);
  13. node1.Attributes.Append(doc.CreateAttribute("name")).InnerText = "文明3";
  14.  
  15. node1.AppendChild(doc.CreateNode(XmlNodeType.Element,"Price",null)).InnerText = "100";
  16. XmlNode node2 = doc.CreateNode(XmlNodeType.Element,"www-microsoft-game");
  17. rootNode.AppendChild(node2);
  18. node2.Attributes.Append(doc.CreateAttribute("name")).InnerText = "帝国时代";
  19.  
  20. node2.AppendChild(doc.CreateNode(XmlNodeType.Element,null)).InnerText = "300";
  21. doc.InsertBefore(declaration,doc.DocumentElement);
  22. doc.Save("game.xml");
  23. }

2、插入节点

  1. /// <summary>
  2. /// 2.插入节点
  3. /// </summary>
  4. public static void InsertNode()
  5. {
  6. //1.加载XML document
  7. XmlDocument doc = new XmlDocument();
  8. doc.Load(@"game.xml");
  9. //Get the root element
  10. XmlNode rootNode = doc.DocumentElement;
  11.  
  12. //create the new game
  13. XmlNode newNode = doc.CreateNode(XmlNodeType.Element,"www-microsoft-game");
  14. rootNode.AppendChild(newNode);
  15. newNode.Attributes.Append(doc.CreateAttribute("name")).InnerText = "帝国时代X";
  16.  
  17. newNode.AppendChild(doc.CreateNode(XmlNodeType.Element,null)).InnerText = "300";
  18.  
  19. doc.Save("newgame.xml");
  20.  
  21. }

3、删除节点

  1. /// <summary>
  2. /// 3.删除节点
  3. /// </summary>
  4. public static void DeleteNode()
  5. {
  6. XmlDocument doc = new XmlDocument();
  7.  
  8. doc.Load("newGame.xml");
  9.  
  10. XmlNode root = doc.DocumentElement;
  11. if (root.HasChildNodes)
  12. {
  13. XmlNode game = root.LastChild;
  14. root.RemoveChild(game);
  15. doc.Save("newGame2.xml");
  16. }
  17.  
  18. }

4、更新节点

  1. /// <summary>
  2. /// 4.更新节点
  3. /// </summary>
  4. public static void UpdateNode()
  5. {
  6. XmlDocument doc = new XmlDocument();
  7. doc.Load("game.xml");
  8. XmlNode root = doc.DocumentElement;
  9. XmlNamespaceManager nsmgr =
  10. new XmlNamespaceManager(
  11. new XmlDocument().NaMetable);
  12. //建立Xml命名空间管理器对象
  13. nsmgr.AddNamespace("v","www-microsoft-game");
  14. //XmlNode updateNode = root.SelectSingleNode()
  15. XmlNode updateNode = doc.SelectSingleNode("v:Games/v:Game[@name='文明3']/Price",nsmgr);
  16.  
  17. updateNode.InnerText =" 330";
  18. doc.Save("gamex.xml");
  19.  
  20. }

参考资料:C# 操作XML之读取Xml浅析,http://developer.51cto.com/art/200908/144648.htm

C# 操作XML之建立Xml对象浅析 ,http://developer.51cto.com/art/200908/144652.htm

C#入门经典(第五版)中文版,第22章 XML p623-p645

猜你在找的XML相关文章