---
title: " C#操作XML的完整例子——XmlDocument篇"
author: "daniel_kwok"
date: "2008-10-15T01:02:42Z"
tags: []
slug: "c-xml-xmldocument"
draft: false
lastmod: "2008-10-15T01:02:42Z"
id: 252931
url: "https://dkstudio.iteye.com/blog/252931"
---

这是一个用c#控制台程序下，  用XmlDocument 进行XML操作的的例子，包含了查询、增加、修改、删除、保存的基本操作。较完整的描述了一个XML的整个操作流程。适合刚入门.net XML操作的朋友参考和学习。

假设有XML文件：books.xml

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;books&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;book&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;name&gt;哈里波特&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;price&gt;10&lt;/price&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;memo&gt;这是一本很好看的书。&lt;/memo&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/book&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;book id="B02"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;name&gt;三国演义&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;price&gt;10&lt;/price&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;memo&gt;四大名著之一。&lt;/memo&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/book&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;book id="B03"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;name&gt;水浒&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;price&gt;6&lt;/price&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;memo&gt;四大名著之一。&lt;/memo&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/book&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;book id="B04"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;name&gt;红楼&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;price&gt;5&lt;/price&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;memo&gt;四大名著之一。&lt;/memo&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/book&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/books&gt;    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

下面是为Program.cs

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System.Collections.Generic;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System.Text;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System.Xml;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)namespace TestXml  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    class Program  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static void Main(string\[] args)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            XmlElement theBook = null, theElem = null, root = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            XmlDocument xmldoc = new XmlDocument();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                xmldoc.Load("Books.xml");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                root = xmldoc.DocumentElement;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //---  新建一本书开始 ----  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theBook = xmldoc.CreateElement("book");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theElem = xmldoc.CreateElement("name");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theElem.InnerText = "新书";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theBook.AppendChild(theElem);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theElem = xmldoc.CreateElement("price");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theElem.InnerText = "20";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theBook.AppendChild(theElem);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theElem = xmldoc.CreateElement("memo");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theElem.InnerText = "新书更好看。";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theBook.AppendChild(theElem);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                root.AppendChild(theBook);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine("---  新建一本书开始 ----");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine(root.OuterXml);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //---  新建一本书完成 ----  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //---  下面对《哈里波特》做一些修改。 ----  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //---  查询找《哈里波特》----  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theBook = (XmlElement)root.SelectSingleNode("/books/book\[name='哈里波特']");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine("---  查找《哈里波特》 ----");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine(theBook.OuterXml);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //---  此时修改这本书的价格 -----  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theBook.GetElementsByTagName("price").Item(0).InnerText = "15";//getElementsByTagName返回的是NodeList，所以要跟上item(0)。另外，GetElementsByTagName("price")相当于SelectNodes(".//price")。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine("---  此时修改这本书的价格 ----");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine(theBook.OuterXml);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //---  另外还想加一个属性id，值为B01 ----  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theBook.SetAttribute("id", "B01");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine("---  另外还想加一个属性id，值为B01 ----");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine(theBook.OuterXml);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //---  对《哈里波特》修改完成。 ----  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //---  再将所有价格低于10的书删除  ----  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theBook = (XmlElement)root.SelectSingleNode("/books/book\[@id='B02']");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine("---  要用id属性删除《三国演义》这本书 ----");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine(theBook.OuterXml);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                theBook.ParentNode.RemoveChild(theBook);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine("---  删除后的ＸＭＬ ----");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine(xmldoc.OuterXml);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //---  再将所有价格低于10的书删除  ----  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                XmlNodeList someBooks = root.SelectNodes("/books/book\[price&lt;10]");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine("---  再将所有价格低于10的书删除  ---");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine("---  符合条件的书有　" + someBooks.Count + "本。  ---");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                for (int i = 0; i &lt; someBooks.Count; i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    someBooks.Item(i).ParentNode.RemoveChild(someBooks.Item(i));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine("---  删除后的ＸＭＬ ----");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine(xmldoc.OuterXml);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                xmldoc.Save("books.xml");//保存到books.xml  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.In.Read();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            catch (Exception e)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Console.Out.WriteLine(e.Message);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}