首先要创建一个XML文件,XML文件的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book Type="必修课" ISBN="7-111-19149-2">
<title>数据结构</title>
<author>严蔚敏</author>
<price>30.00</price>
</book>
<book Type="必修课" ISBN="7-111-19149-3">
<title>路由型与交换型互联网基础</title>
<author>程庆梅</author>
<price>27.00</price>
</book>
<book Type="必修课" ISBN="7-111-19149-5">
<title>软件质量保证与管理</title>
<author>朱少民</author>
<price>39.00</price>
</book>
<book Type="必修课" ISBN="7-111-19149-6">
<title>算法设计与分析</title>
<author>王红梅</author>
<price>23.00</price>
</book>
<book Type="选修课" ISBN="7-111-19149-7">
<title>C语言程序设计</title>
<author>谭浩强</author>
<price>48.5</price>
</book>
</bookstore>
为了方便读取,我们定义一个书的实体类,具体代码如下:
/// <summary>
/// 课程类型
/// </summary>
private string bookType;
public string BookType
{
get { return bookType; }
set { bookType = value; }
}
/// <summary>
/// ISBN号
/// </summary>
private string bookISBN;
public string BookISBN
{
get { return bookISBN; }
set { bookISBN = value; }
}
/// <summary>
/// 书的名称
/// </summary>
private string bookName;
public string BookName
{
get { return bookName; }
set { bookName = value; }
}
/// <summary>
/// 作者
/// </summary>
private string bookAuthor;
public string BookAuthor
{
get { return bookAuthor; }
set { bookAuthor = value; }
}
/// <summary>
/// 价格
/// </summary>
private double bookPrice;
public double BookPrice
{
get { return bookPrice; }
set { bookPrice = value; }
}
接着定义操作XML文件的读、写、修改、删除等操作的方法。
1、读取操作的方法定义如下:
public static List<BookModel> ReadAllXml()
{
//实例化一个xml文档对象
XmlDocument doc = new XmlDocument();
//读取过程忽略文档中的注释
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(HttpContext.Current.Server.MapPath("App_Data/Books.xml"), settings);
doc.Load(reader);
//得到根节点bookstore
XmlNode xn = doc.SelectSingleNode("bookstore");
//得到根节点的所有子节点
XmlNodeList xnl = xn.ChildNodes;
//把读取出来的内容放到一个list中
List<BookModel> bookModeList = new List<BookModel>();
foreach (XmlNode xn1 in xnl)
{
//实例化一个模型
BookModel bookModel = new BookModel();
//将节点转换为元素,便于得到节点的属性值
XmlElement xe = (XmlElement)xn1;
//得到Type和ISBN两个属性的属性值
bookModel.BookISBN = xe.GetAttribute("ISBN").ToString();
bookModel.BookType = xe.GetAttribute("Type").ToString();
//得到Book节点的所有子节点的值
XmlNodeList xnl0 = xe.ChildNodes;
bookModel.BookName = xnl0.Item(0).InnerText; //BookName在XML中处于第1个子结点
bookModel.BookAuthor = xnl0.Item(1).InnerText; //BookAuthor在XML中处于第2个子结点
bookModel.BookPrice = Convert.ToDouble(xnl0.Item(2).InnerText);//BookPrice在XML中处于第3个子结点
bookModeList.Add(bookModel);
}
reader.Close();
return bookModeList;
}
2、写入操作的方法定义如下:
public static void Add(BookModel books)
{
//实例化一个xml文档对象
XmlDocument doc = new XmlDocument();
//读取过程忽略文档中的注释
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(HttpContext.Current.Server.MapPath("App_Data/Books.xml"), settings);
doc.Load(reader);
//读取根结点
XmlNode xn = doc.SelectSingleNode("bookstore");
//创建一个新的book结点,并设置结点的属性
XmlElement xelKey = doc.CreateElement("book");
XmlAttribute xelType1 = doc.CreateAttribute("Type");
XmlAttribute xelType2 = doc.CreateAttribute("ISBN");
xelType1.InnerText = books.BookType;
xelType2.InnerText = books.BookISBN;
xelKey.SetAttributeNode(xelType1);
xelKey.SetAttributeNode(xelType2);
//创建title子结点
XmlElement xelTitle = doc.CreateElement("title");
xelTitle.InnerText = books.BookAuthor;
xelKey.AppendChild(xelTitle);
//创建author子结点
XmlElement xelAuthor = doc.CreateElement("author");
xelAuthor.InnerText = books.BookAuthor;
xelKey.AppendChild(xelAuthor);
//创建price子结点
XmlElement xelPrice = doc.CreateElement("price");
xelPrice.InnerText = books.BookPrice.ToString();
xelKey.AppendChild(xelPrice);
//把book结点挂接已有book结点的末尾
xn.AppendChild(xelKey);
reader.Close();//先关闭reader,再执行保存操作
//最后保存整个文件
doc.Save(HttpContext.Current.Server.MapPath("App_Data/Books.xml"));
}
3、修改操作的方法定义如下:
/// <summary>
/// 根据ISBN号修改结点内容
/// </summary>
/// <param name="book">书的模型</param>
public static void Update(BookModel book)
{
//实例化一个xml文档对象
XmlDocument doc = new XmlDocument();
//读取过程忽略文档中的注释
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(HttpContext.Current.Server.MapPath("App_Data/Books.xml"), settings);
doc.Load(reader);
//DocumentElement 获取xml文档对象的根XmlElement.
XmlElement xe = doc.DocumentElement;
string strPath = string.Format("/bookstore/book[@ISBN=\"{0}\"]", book.BookISBN);//ISBN不能修改,作为修改的依据
XmlElement selectXe = (XmlElement)xe.SelectSingleNode(strPath); //selectSingleNode 根据XPath表达式,获得符合条件的第一个节点.
selectXe.SetAttribute("Type", book.BookType);//也可以通过SetAttribute来增加一个属性
selectXe.GetElementsByTagName("title").Item(0).InnerText = book.BookName;
selectXe.GetElementsByTagName("author").Item(0).InnerText = book.BookAuthor;
selectXe.GetElementsByTagName("price").Item(0).InnerText = book.BookPrice.ToString();
reader.Close();//先关闭reader,再执行保存操作
//最后保存整个文件
doc.Save(HttpContext.Current.Server.MapPath("App_Data/Books.xml"));
}
4、删除操作的方法定义如下:
/// <summary>
/// 根据ISBN号删除结点
/// </summary>
/// <param name="isbn">ISBN号</param>
public static void Delete(string isbn)
{
//实例化一个xml文档对象
XmlDocument doc = new XmlDocument();
//读取过程忽略文档中的注释
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(HttpContext.Current.Server.MapPath("App_Data/Books.xml"), settings);
doc.Load(reader);
XmlElement xe = doc.DocumentElement; // DocumentElement 获取xml文档对象的根XmlElement.
string strPath = string.Format("/bookstore/book[@ISBN=\"{0}\"]", isbn);
XmlElement selectXe = (XmlElement)xe.SelectSingleNode(strPath); //selectSingleNode 根据XPath表达式,获得符合条件的第一个节点.
selectXe.ParentNode.RemoveChild(selectXe);
reader.Close();//先关闭reader,再执行保存操作
//最后保存整个文件
doc.Save(HttpContext.Current.Server.MapPath("App_Data/Books.xml"));
}
最后到页面调用以上方法即可。
