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

XML文件是一種以簡單文本格式存儲數據的方式。下面介紹XML文件的幾中基本操作。
1、新建XML文件


        /// <summary>
        /// 1.新建XML文件
        /// </summary>
        public static  void CreateXML()
        {
            XmlDocument doc = new XmlDocument();
            //xml declaration (xml聲明)
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlNode rootNode = doc.CreateNode(XmlNodeType.Element, "v", "Games", "www-microsoft-game");
            doc.AppendChild(rootNode);
            XmlNode node1 = doc.CreateNode(XmlNodeType.Element, "v", "Game", "www-microsoft-game");
            rootNode.AppendChild(node1);
            node1.Attributes.Append(doc.CreateAttribute("name")).InnerText = "文明3";

            node1.AppendChild(doc.CreateNode(XmlNodeType.Element, "Price", null)).InnerText = "100";
            XmlNode node2 = doc.CreateNode(XmlNodeType.Element, "v", "Game", "www-microsoft-game");
            rootNode.AppendChild(node2);
            node2.Attributes.Append(doc.CreateAttribute("name")).InnerText = "帝國時代";

            node2.AppendChild(doc.CreateNode(XmlNodeType.Element, "Price", null)).InnerText = "300";
            doc.InsertBefore(declaration, doc.DocumentElement);
            doc.Save("game.xml");
        }

2、插入節點

/// <summary>
        /// 2.插入節點
        /// </summary>
        public static void InsertNode()
        {
            //1.加載XML document
            XmlDocument doc = new XmlDocument();
            doc.Load(@"game.xml");
            //Get the root element
            XmlNode  rootNode = doc.DocumentElement;

            //create the new game
            XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "v", "Game", "www-microsoft-game");
            rootNode.AppendChild(newNode);
            newNode.Attributes.Append(doc.CreateAttribute("name")).InnerText = "帝國時代X";

            newNode.AppendChild(doc.CreateNode(XmlNodeType.Element, "Price", null)).InnerText = "300";

            doc.Save("newgame.xml");

        }

3、刪除節點

/// <summary>
        /// 3.刪除節點
        /// </summary>
        public static void DeleteNode()
        {
            XmlDocument doc = new XmlDocument();

            doc.Load("newGame.xml");

            XmlNode root = doc.DocumentElement;
            if (root.HasChildNodes)
            {
                XmlNode game = root.LastChild;
                root.RemoveChild(game);
                doc.Save("newGame2.xml");
            }

        }

4、更新節點

       /// <summary>
        /// 4.更新節點
        /// </summary>
        public static void UpdateNode()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("game.xml");
            XmlNode root = doc.DocumentElement;
            XmlNamespaceManager nsmgr =
new XmlNamespaceManager(
new XmlDocument().NameTable);
            //建立Xml命名空間管理器對象  
            nsmgr.AddNamespace("v", "www-microsoft-game");  
            //XmlNode updateNode = root.SelectSingleNode()
           XmlNode updateNode = doc.SelectSingleNode("v:Games/v:Game[@name='文明3']/Price",nsmgr);

           updateNode.InnerText =" 330";
           doc.Save("gamex.xml");

        }

參考資料: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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章