C#實(shí)現(xiàn)XML文檔的增刪改查功能示例
本文實(shí)例講述了C#實(shí)現(xiàn)XML文檔的增刪改查功能。分享給大家供大家參考,具體如下:
1、 創(chuàng)建實(shí)例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、 創(chuàng)建圖書信息實(shí)體類(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>
/// 圖書語(yǔ)言
/// </summary>
public string Language { set; get; }
/// <summary>
/// 圖書作者
/// </summary>
public string Author { set; get; }
/// <summary>
/// 出版時(shí)間
/// </summary>
public string Year { set; get; }
/// <summary>
/// 銷售價(jià)格
/// </summary>
public decimal Price { set; get; }
}
3、 創(chuàng)建圖書信息業(yè)務(wù)邏輯類(BookInfoBLL.cs)
using System.Xml; //引用相關(guān)文件
public class BookInfoBLL
{
private string _basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/xml/Books.xml"; //XML文件路徑
private XmlDocument _booksXmlDoc = null; //創(chuàng)建XML文檔對(duì)象
public BookInfoBLL()
{
try
{
_booksXmlDoc = new XmlDocument(); //初始化XML文檔對(duì)象
_booksXmlDoc.Load(_basePath); //加載指定的XML文檔
}
catch (Exception ex)
{
throw new Exception("加載XML文檔出錯(cuò):" + ex.Message);
}
}
/// <summary>
/// 獲取圖書列表(查)
/// </summary>
/// <param name="param">參數(shù)條件</param>
/// <returns>圖書列表</returns>
public List<BookInfo> GetBookInfoList(BookInfo param)
{
List<BookInfo> bookInfoList = new List<BookInfo>();
string xPath = "bookstore/book"; //默認(rèn)獲取所有圖書
if (param.BookId != 0) //根據(jù)圖書ID查詢
{
xPath = String.Format("/bookstore/book[@id='{0}']", param.BookId);
}
else if (!String.IsNullOrEmpty(param.Category)) //根據(jù)圖書類別查詢
{
xPath = String.Format("/bookstore/book[@category='{0}']", param.Category);
}
else if (!String.IsNullOrEmpty(param.Title)) //根據(jù)圖書名稱查詢
{
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; //獲取子節(jié)點(diǎn)的屬性值
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>
//創(chuàng)建節(jié)點(diǎn)
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");
//給節(jié)點(diǎn)賦值
bookXmlElement.SetAttribute("id", param.BookId.ToString());
bookXmlElement.SetAttribute("category", param.Category);
titleXmlElement.InnerText = param.Title; //給節(jié)點(diǎn)添加元素值
titleXmlElement.SetAttribute("lang", param.Language);//給節(jié)點(diǎn)添加屬性值
authorXmlElement.InnerText = param.Author;
yearXmlElement.InnerText = param.Year;
priceXmlElement.InnerText = param.Price.ToString();
//AppendChild 將指定的節(jié)點(diǎn)添加到該節(jié)點(diǎn)的子節(jié)點(diǎn)列表的末尾
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); //移除指定的子節(jié)點(diǎn)
_booksXmlDoc.Save(_basePath);
result = true;
}
}
return result;
}
}
PS:這里再為大家提供幾款比較實(shí)用的xml相關(guān)在線工具供大家使用:
在線XML格式化/壓縮工具:
http://tools.jb51.net/code/xmlformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
上一篇:微信開發(fā)--企業(yè)轉(zhuǎn)賬到用戶
欄 目:C#教程
下一篇:C# 最齊全的上傳圖片方法
本文標(biāo)題:C#實(shí)現(xiàn)XML文檔的增刪改查功能示例
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5995.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery


