C#操作XML的完整例子——XmlDocument篇
这是一个用c#控制台程序下, 用XmlDocument 进行XML操作的的例子,包含了查询、增加、修改、删除、保存的基本操作。较完整的描述了一个XML的整个操作流程。适合刚入门.net XML操作的朋友参考和学习。
假设有XML文件:books.xml
 <?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?> <books>
<books> <book>
 <book> <name>哈里波特</name>
  <name>哈里波特</name> <price>10</price>
  <price>10</price> <memo>这是一本很好看的书。</memo>
  <memo>这是一本很好看的书。</memo> </book>
 </book> <book id="B02">
 <book id="B02"> <name>三国演义</name>
  <name>三国演义</name> <price>10</price>
  <price>10</price> <memo>四大名著之一。</memo>
  <memo>四大名著之一。</memo> </book>
 </book> <book id="B03">
 <book id="B03"> <name>水浒</name>
  <name>水浒</name> <price>6</price>
  <price>6</price> <memo>四大名著之一。</memo>
  <memo>四大名著之一。</memo> </book>
 </book> <book id="B04">
 <book id="B04"> <name>红楼</name>
  <name>红楼</name> <price>5</price>
  <price>5</price> <memo>四大名著之一。</memo>
  <memo>四大名著之一。</memo> </book>
 </book> </books>
</books>  
下面是为Program.cs
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Text;
using System.Text; using System.Xml;
using System.Xml;
 namespace TestXml
namespace TestXml {
{ class Program
    class Program {
    { static void Main(string[] args)
        static void Main(string[] args) {
        { XmlElement theBook = null, theElem = null, root = null;
            XmlElement theBook = null, theElem = null, root = null; XmlDocument xmldoc = new XmlDocument();
            XmlDocument xmldoc = new XmlDocument(); try
            try {
            { xmldoc.Load("Books.xml");
                xmldoc.Load("Books.xml"); root = xmldoc.DocumentElement;
                root = xmldoc.DocumentElement;
 //---  新建一本书开始 ----
                //---  新建一本书开始 ---- theBook = xmldoc.CreateElement("book");
                theBook = xmldoc.CreateElement("book"); theElem = xmldoc.CreateElement("name");
                theElem = xmldoc.CreateElement("name"); theElem.InnerText = "新书";
                theElem.InnerText = "新书"; theBook.AppendChild(theElem);
                theBook.AppendChild(theElem);
 theElem = xmldoc.CreateElement("price");
                theElem = xmldoc.CreateElement("price"); theElem.InnerText = "20";
                theElem.InnerText = "20"; theBook.AppendChild(theElem);
                theBook.AppendChild(theElem);
 theElem = xmldoc.CreateElement("memo");
                theElem = xmldoc.CreateElement("memo"); theElem.InnerText = "新书更好看。";
                theElem.InnerText = "新书更好看。"; theBook.AppendChild(theElem);
                theBook.AppendChild(theElem); root.AppendChild(theBook);
                root.AppendChild(theBook); Console.Out.WriteLine("---  新建一本书开始 ----");
                Console.Out.WriteLine("---  新建一本书开始 ----"); Console.Out.WriteLine(root.OuterXml);
                Console.Out.WriteLine(root.OuterXml); //---  新建一本书完成 ----
                //---  新建一本书完成 ----
 //---  下面对《哈里波特》做一些修改。 ----
                //---  下面对《哈里波特》做一些修改。 ---- //---  查询找《哈里波特》----
                //---  查询找《哈里波特》---- theBook = (XmlElement)root.SelectSingleNode("/books/book[name='哈里波特']");
                theBook = (XmlElement)root.SelectSingleNode("/books/book[name='哈里波特']"); Console.Out.WriteLine("---  查找《哈里波特》 ----");
                Console.Out.WriteLine("---  查找《哈里波特》 ----"); Console.Out.WriteLine(theBook.OuterXml);
                Console.Out.WriteLine(theBook.OuterXml); //---  此时修改这本书的价格 -----
                //---  此时修改这本书的价格 ----- theBook.GetElementsByTagName("price").Item(0).InnerText = "15";//getElementsByTagName返回的是NodeList,所以要跟上item(0)。另外,GetElementsByTagName("price")相当于SelectNodes(".//price")。
                theBook.GetElementsByTagName("price").Item(0).InnerText = "15";//getElementsByTagName返回的是NodeList,所以要跟上item(0)。另外,GetElementsByTagName("price")相当于SelectNodes(".//price")。 Console.Out.WriteLine("---  此时修改这本书的价格 ----");
                Console.Out.WriteLine("---  此时修改这本书的价格 ----"); Console.Out.WriteLine(theBook.OuterXml);
                Console.Out.WriteLine(theBook.OuterXml); //---  另外还想加一个属性id,值为B01 ----
                //---  另外还想加一个属性id,值为B01 ---- theBook.SetAttribute("id", "B01");
                theBook.SetAttribute("id", "B01"); Console.Out.WriteLine("---  另外还想加一个属性id,值为B01 ----");
                Console.Out.WriteLine("---  另外还想加一个属性id,值为B01 ----"); Console.Out.WriteLine(theBook.OuterXml);
                Console.Out.WriteLine(theBook.OuterXml); //---  对《哈里波特》修改完成。 ----
                //---  对《哈里波特》修改完成。 ----
 //---  再将所有价格低于10的书删除  ----
                //---  再将所有价格低于10的书删除  ---- theBook = (XmlElement)root.SelectSingleNode("/books/book[@id='B02']");
                theBook = (XmlElement)root.SelectSingleNode("/books/book[@id='B02']"); Console.Out.WriteLine("---  要用id属性删除《三国演义》这本书 ----");
                Console.Out.WriteLine("---  要用id属性删除《三国演义》这本书 ----"); Console.Out.WriteLine(theBook.OuterXml);
                Console.Out.WriteLine(theBook.OuterXml); theBook.ParentNode.RemoveChild(theBook);
                theBook.ParentNode.RemoveChild(theBook); Console.Out.WriteLine("---  删除后的XML ----");
                Console.Out.WriteLine("---  删除后的XML ----"); Console.Out.WriteLine(xmldoc.OuterXml);
                Console.Out.WriteLine(xmldoc.OuterXml);
 //---  再将所有价格低于10的书删除  ----
                //---  再将所有价格低于10的书删除  ---- XmlNodeList someBooks = root.SelectNodes("/books/book[price<10]");
                XmlNodeList someBooks = root.SelectNodes("/books/book[price<10]"); Console.Out.WriteLine("---  再将所有价格低于10的书删除  ---");
                Console.Out.WriteLine("---  再将所有价格低于10的书删除  ---"); Console.Out.WriteLine("---  符合条件的书有 " + someBooks.Count + "本。  ---");
                Console.Out.WriteLine("---  符合条件的书有 " + someBooks.Count + "本。  ---");
 for (int i = 0; i < someBooks.Count; i++)
                for (int i = 0; i < someBooks.Count; i++) {
                { someBooks.Item(i).ParentNode.RemoveChild(someBooks.Item(i));
                    someBooks.Item(i).ParentNode.RemoveChild(someBooks.Item(i)); }
                } Console.Out.WriteLine("---  删除后的XML ----");
                Console.Out.WriteLine("---  删除后的XML ----"); Console.Out.WriteLine(xmldoc.OuterXml);
                Console.Out.WriteLine(xmldoc.OuterXml);
 xmldoc.Save("books.xml");//保存到books.xml
                xmldoc.Save("books.xml");//保存到books.xml
 Console.In.Read();
                Console.In.Read(); }
            } catch (Exception e)
            catch (Exception e) {
            { Console.Out.WriteLine(e.Message);
                Console.Out.WriteLine(e.Message); }
            } }
        } }
    } }
} 
                    
                

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号