最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)XML文檔的增刪改查功能示例

 更新時間:2017年01月24日 08:46:00   作者:pan_junbiao  
這篇文章主要介紹了C#實現(xiàn)XML文檔的增刪改查功能,結合實例形式分析了xml文檔的創(chuàng)建及C#針對xml文檔的加載及增刪改查等操作技巧,需要的朋友可以參考下

本文實例講述了C#實現(xiàn)XML文檔的增刪改查功能。分享給大家供大家參考,具體如下:

1、 創(chuàng)建實例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)建圖書信息實體類(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、 創(chuàng)建圖書信息業(yè)務邏輯類(BookInfoBLL.cs)

using System.Xml;  //引用相關文件
public class BookInfoBLL
{
  private string _basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/xml/Books.xml"; //XML文件路徑
  private XmlDocument _booksXmlDoc = null;  //創(chuàng)建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">參數(shù)條件</param>
  /// <returns>圖書列表</returns>
  public List<BookInfo> GetBookInfoList(BookInfo param)
  {
    List<BookInfo> bookInfoList = new List<BookInfo>();
    string xPath = "bookstore/book"; //默認獲取所有圖書
    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é)點的屬性值
      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é)點
    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é)點賦值
    bookXmlElement.SetAttribute("id", param.BookId.ToString());
    bookXmlElement.SetAttribute("category", param.Category);
    titleXmlElement.InnerText = param.Title; //給節(jié)點添加元素值
    titleXmlElement.SetAttribute("lang", param.Language);//給節(jié)點添加屬性值
    authorXmlElement.InnerText = param.Author;
    yearXmlElement.InnerText = param.Year;
    priceXmlElement.InnerText = param.Price.ToString();
    //AppendChild 將指定的節(jié)點添加到該節(jié)點的子節(jié)點列表的末尾
    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é)點
        _booksXmlDoc.Save(_basePath);
        result = true;
      }
    }
    return result;
  }
}

PS:這里再為大家提供幾款比較實用的xml相關在線工具供大家使用:

在線XML格式化/壓縮工具:
http://tools.jb51.net/code/xmlformat

在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljson

XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多關于C#相關內容感興趣的讀者可查看本站專題:《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《C#程序設計之線程使用技巧總結》、《WinForm控件用法總結》、《C#數(shù)據(jù)結構與算法教程》、《C#數(shù)組操作技巧總結》及《C#面向對象程序設計入門教程

希望本文所述對大家C#程序設計有所幫助。

相關文章

  • C#遞歸方法實現(xiàn)無限級分類顯示效果實例

    C#遞歸方法實現(xiàn)無限級分類顯示效果實例

    這篇文章主要介紹了C#遞歸方法實現(xiàn)無限級分類顯示效果,結合完整實例形式分析了C#遞歸算法與數(shù)據(jù)元素遍歷的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • C#中參數(shù)數(shù)組、引用參數(shù)和輸出參數(shù)示例詳解

    C#中參數(shù)數(shù)組、引用參數(shù)和輸出參數(shù)示例詳解

    這篇文章主要給大家介紹了關于C#中參數(shù)數(shù)組、引用參數(shù)和輸出參數(shù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-05-05
  • C#獲取ListView鼠標下的Item實例

    C#獲取ListView鼠標下的Item實例

    下面小編就為大家?guī)硪黄狢#獲取ListView鼠標下的Item實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • 深入淺出掌握Unity ShaderLab語法基礎

    深入淺出掌握Unity ShaderLab語法基礎

    Unity中所有Shader文件都通過一種陳述性語言進行描述,稱為“ShaderLab”, 這篇文章主要介紹了Unity圖形學之ShaderLab入門基礎,需要的朋友可以參考下
    2023-05-05
  • C#寫日志類實例

    C#寫日志類實例

    這篇文章主要介紹了C#寫日志類,實現(xiàn)將日志信息寫入文本文件的功能,非常具有實用價值,需要的朋友可以參考下
    2014-10-10
  • C#中SerialPort的使用教程詳解

    C#中SerialPort的使用教程詳解

    SerilPort是串口進行數(shù)據(jù)通信的一個控件,這篇文章主要為大家詳細介紹了C#中SerialPort的使用,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-12-12
  • c#隱藏基類方法的作用

    c#隱藏基類方法的作用

    這篇文章主要介紹了c#隱藏基類方法的作用,大家可以參考使用
    2013-12-12
  • Unity使用DoTween實現(xiàn)拋物線效果

    Unity使用DoTween實現(xiàn)拋物線效果

    這篇文章主要為大家詳細介紹了Unity使用DoTween實現(xiàn)拋物線效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • 用C#縮小照片上傳到各種空間的具體方法

    用C#縮小照片上傳到各種空間的具體方法

    這篇文章介紹了用C#縮小照片的具體方法,有需要的朋友可以參考一下
    2013-09-09
  • Unity向量按照某一點進行旋轉

    Unity向量按照某一點進行旋轉

    這篇文章主要為大家詳細介紹了Unity向量按照某一點進行旋轉,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-01-01

最新評論

鹤岗市| 隆化县| 苗栗县| 荥阳市| 亳州市| 蒲城县| 屏山县| 综艺| 黄骅市| 习水县| 高邮市| 麻栗坡县| 始兴县| 扶风县| 射阳县| 庆元县| 永城市| 濮阳市| 东安县| 黄骅市| 九寨沟县| 济阳县| 韩城市| 墨玉县| 松桃| 报价| 子洲县| 塔河县| 新巴尔虎右旗| 旬阳县| 西林县| 河源市| 清流县| 洱源县| 壤塘县| 临江市| 新平| 英山县| 玉树县| 威远县| 桑日县|