這篇文章主要介紹了C#實現XML文檔的增刪改查功能,結合執行個體形式分析了xml文檔的建立及C#針對xml文檔的載入及增刪改查等操作技巧,需要的朋友可以參考下
本文執行個體講述了C#實現XML文檔的增刪改查功能。分享給大家供大家參考,具體如下:
1、 建立執行個體XML檔案(Books.xml)
<?xml version="1.0" encoding="iso-8859-1"?><bookstore> <book id="1" category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book id="2" category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book id="3" category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book id="4" category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book></bookstore>
2、 建立圖書資訊實體類(BookInfo.cs)
public class BookInfo{ /// <summary> /// 圖書ID /// </summary> public int BookId { set; get; } /// <summary> /// 圖書名稱 /// </summary> public string Title { set; get; } /// <summary> /// 圖書分類 /// </summary> public string Category { set; get; } /// <summary> /// 圖書語言 /// </summary> public string Language { set; get; } /// <summary> /// 圖書作者 /// </summary> public string Author { set; get; } /// <summary> /// 出版時間 /// </summary> public string Year { set; get; } /// <summary> /// 銷售價格 /// </summary> public decimal Price { set; get; }}
3、 建立圖書資訊商務邏輯類(BookInfoBLL.cs)
using System.Xml; //引用相關檔案public class BookInfoBLL{ private string _basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/xml/Books.xml"; //XML檔案路徑 private XmlDocument _booksXmlDoc = null; //建立XML文檔對象 public BookInfoBLL() { try { _booksXmlDoc = new XmlDocument(); //初始化XML文檔對象 _booksXmlDoc.Load(_basePath); //載入指定的XML文檔 } catch (Exception ex) { throw new Exception("載入XML文檔出錯:" + ex.Message); } } /// <summary> /// 擷取圖書列表(查) /// </summary> /// <param name="param">參數條件</param> /// <returns>圖書列表</returns> public List<BookInfo> GetBookInfoList(BookInfo param) { List<BookInfo> bookInfoList = new List<BookInfo>(); string xPath = "bookstore/book"; //預設擷取所有圖書 if (param.BookId != 0) //根據圖書ID查詢 { xPath = String.Format("/bookstore/book[@id='{0}']", param.BookId); } else if (!String.IsNullOrEmpty(param.Category)) //根據圖書類別查詢 { xPath = String.Format("/bookstore/book[@category='{0}']", param.Category); } else if (!String.IsNullOrEmpty(param.Title)) //根據圖書名稱查詢 { xPath = String.Format("/bookstore/book[title='{0}']", param.Title); } XmlNodeList booksXmlNodeList = _booksXmlDoc.SelectNodes(xPath); foreach (XmlNode bookNode in booksXmlNodeList) { BookInfo bookInfo = new BookInfo(); bookInfo.BookId = Convert.ToInt32(bookNode.Attributes["id"].Value); //擷取屬性值 bookInfo.Category = bookNode.Attributes["category"].Value; bookInfo.Language = bookNode.SelectSingleNode("title").Attributes["lang"].Value; //擷取子節點的屬性值 bookInfo.Title = bookNode.SelectSingleNode("title").InnerText; //擷取元素值 bookInfo.Author = bookNode.SelectSingleNode("author").InnerText; bookInfo.Year = bookNode.SelectSingleNode("year").InnerText; bookInfo.Price = Convert.ToDecimal(bookNode.SelectSingleNode("price").InnerText); bookInfoList.Add(bookInfo); } return bookInfoList; } /// <summary> /// 增加圖書資訊(增) /// </summary> /// <param name="param"></param> /// <returns></returns> public bool AddBookInfo(BookInfo param) { bool result = false; XmlNode root = _booksXmlDoc.SelectSingleNode("bookstore"); //尋找<bookstore> //建立節點 XmlElement bookXmlElement = _booksXmlDoc.CreateElement("book"); XmlElement titleXmlElement = _booksXmlDoc.CreateElement("title"); XmlElement authorXmlElement = _booksXmlDoc.CreateElement("author"); XmlElement yearXmlElement = _booksXmlDoc.CreateElement("year"); XmlElement priceXmlElement = _booksXmlDoc.CreateElement("price"); //給節點賦值 bookXmlElement.SetAttribute("id", param.BookId.ToString()); bookXmlElement.SetAttribute("category", param.Category); titleXmlElement.InnerText = param.Title; //給節點添加元素值 titleXmlElement.SetAttribute("lang", param.Language);//給節點添加屬性值 authorXmlElement.InnerText = param.Author; yearXmlElement.InnerText = param.Year; priceXmlElement.InnerText = param.Price.ToString(); //AppendChild 將指定的節點添加到該節點的子節點列表的末尾 bookXmlElement.AppendChild(titleXmlElement); bookXmlElement.AppendChild(authorXmlElement); bookXmlElement.AppendChild(yearXmlElement); bookXmlElement.AppendChild(priceXmlElement); root.AppendChild(bookXmlElement); _booksXmlDoc.Save(_basePath); result = true; return result; } /// <summary> /// 修改圖書資訊(改) /// </summary> /// <param name="param"></param> /// <returns></returns> public bool EditBookInfo(BookInfo param) { bool result = false; if(param.BookId>0) { string xPath = String.Format("/bookstore/book[@id='{0}']", param.BookId); XmlNode editXmlNode = _booksXmlDoc.SelectSingleNode(xPath); XmlElement editXmlElement = (XmlElement)editXmlNode; if (editXmlElement != null) { editXmlElement.Attributes["category"].Value = param.Category; editXmlElement.SelectSingleNode("title").Attributes["lang"].Value = param.Language; editXmlElement.SelectSingleNode("title").InnerText = param.Title; editXmlElement.SelectSingleNode("author").InnerText = param.Author; editXmlElement.SelectSingleNode("year").InnerText = param.Year; editXmlElement.SelectSingleNode("price").InnerText = param.Price.ToString(); _booksXmlDoc.Save(_basePath); result = true; } } return result; } /// <summary> /// 刪除圖書資訊(刪) /// </summary> /// <param name="param"></param> /// <returns></returns> public bool DeleteBookInfo(BookInfo param) { bool result = false; if (param.BookId > 0) { string xPath = String.Format("/bookstore/book[@id='{0}']", param.BookId); XmlNode delXmlNode = _booksXmlDoc.SelectSingleNode(xPath); if (delXmlNode != null) { _booksXmlDoc.SelectSingleNode("bookstore").RemoveChild(delXmlNode); //移除指定的子節點 _booksXmlDoc.Save(_basePath); result = true; } } return result; }}