![](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/InBlock.gif)
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.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/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/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("--- 删除后的XML ----");
![](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<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 < someBooks.Count; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.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("--- 删除后的XML ----");
![](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/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)
}