成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一個C#數據訪問XML的例子

開發 后端
本文舉例說明了C#數據訪問XML的方法,希望大家從這個例子中學到相關的知識。

在舉C#數據訪問XML的例子之前,首先介紹一些知識和定義。

XML DOM的類所在的命名空間為System.Xml中

XmlNode 表示文檔中的節點,如果這個節點表示XML的文檔的根,就可以從它導航到文檔的任意位置

XmlDocument 常常作為使用XML的***個對象,這個類用于加載和保存磁盤上或者其他位置的數據

XmlElement 表示XML文檔中的一個元素,派生于XmlLinkedNode,XmlLinkedNode派生于XmlNode

XmlAttribute 表示XMl的一個屬性

XmlText 表示開標記和閉標記之間的文本內容

XmlComment 表示一種特殊類型的節點,這種節點不是文檔的一部分,但是為讀者提供部分信息,通常是注釋

XmlNodeList 表示一個節點集合

C#數據訪問XML示例:

XmlDocument document = new XmlDocument();

document.Loda(@"C:\Test\books.xml");

XmlElement element = document.DocumentElement;//返回一個XmlElement實例

示例1:

  1. //創建一個節點  
  2. private void buttonCreateNode_Click(object sender, EventArgs e)  
  3.         {  
  4.             // Load the XML document  
  5.             XmlDocument document = new XmlDocument();  
  6.             document.Load("../../Books.xml");  
  7.  
  8.  
  9.             // Get the root element  
  10.             XmlElement root = document.DocumentElement;  
  11.  
  12.  
  13.             // Create the new nodes  
  14.             XmlElement newBook = document.CreateElement("book");  
  15.             XmlElement newTitle = document.CreateElement("title");  
  16.             XmlElement newAuthor = document.CreateElement("author");  
  17.             XmlElement newCode = document.CreateElement("code");  
  18.             XmlText title = document.CreateTextNode("Beginning Visual C# 3rd Edition");  
  19.             XmlText author = document.CreateTextNode("Karli Watson et al");  
  20.             XmlText code = document.CreateTextNode("1234567890");  
  21.             XmlComment comment = document.CreateComment("This book is the book you are reading");  
  22.  
  23.  
  24.             // Insert the elements  
  25.             newBook.AppendChild(comment);  
  26.             newBook.AppendChild(newTitle);  
  27.             newBook.AppendChild(newAuthor);  
  28.             newBook.AppendChild(newCode);  
  29.             newTitle.AppendChild(title);  
  30.             newAuthor.AppendChild(author);  
  31.             newCode.AppendChild(code);  
  32.             root.InsertAfter(newBook, root.LastChild);  
  33.  
  34.  
  35.             document.Save("../../Books.xml");  
  36.  
  37.  
  38.             listBoxXmlNodes.Items.Clear();  
  39.             RecurseXmlDocument((XmlNode)document.DocumentElement, 0);  
  40.         }  
  41. //刪除一個節點  
  42. private void buttonDeleteNode_Click(object sender, EventArgs e)  
  43.         {  
  44.             // Load the XML document  
  45.             XmlDocument document = new XmlDocument();  
  46.             document.Load("../../Books.xml");  
  47.  
  48.  
  49.             // Get the root element  
  50.             XmlElement root = document.DocumentElement;  
  51.  
  52.  
  53.             // Find the node. root is the < books> tag, so its last child which will be the  
  54.             // last < book> node  
  55.             if (root.HasChildNodes)  
  56.             {  
  57.                 XmlNode book = root.LastChild;  
  58.  
  59.  
  60.                 // Delete the child  
  61.                 root.RemoveChild(book);  
  62.  
  63.  
  64.                 // Save the document back to disk  
  65.                 document.Save("../../Books.xml");  
  66.                 listBoxXmlNodes.Items.Clear();  
  67.  
  68.  
  69.                 RecurseXmlDocument((XmlNode)document.DocumentElement, 0);  
  70.             }  
  71.         }  
  72. //在一個ListBox中顯示文檔的所有節點名稱以及文本節點的內容  
  73. private void RecurseXmlDocument(XmlNode root, int indent)  
  74.     {  
  75.       // Make sure we don't do anything if the root is null  
  76.       if (root == null)  
  77.         return;  
  78.  
  79.  
  80.       if (root is XmlElement) // Root is an XmlElement type  
  81.       {  
  82.         // first, print the name  
  83.         listBoxXmlNodes.Items.Add(root.Name.PadLeft(root.Name.Length + indent));  
  84.  
  85.  
  86.         // Then check if there are any child nodes and if there are, call this  
  87.         // method again to print them  
  88.         if (root.HasChildNodes)  
  89.           RecurseXmlDocument(root.FirstChild, indent + 2);  
  90.  
  91.  
  92.         // Finally check to see if there are any siblings and if there are  
  93.         // call this method again to have them printed  
  94.         if (root.NextSibling != null)  
  95.           RecurseXmlDocument(root.NextSibling, indent);  
  96.       }  
  97.       else if (root is XmlText)  
  98.       {  
  99.         // Print the text  
  100.         string text = ((XmlText)root).Value;  
  101.         listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));  
  102.       }  
  103.       else if (root is XmlComment)  
  104.       {  
  105.         // Print text  
  106.         string text = root.Value;  
  107.         listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));  
  108.  
  109.  
  110.         // Then check if there are any child nodes and if there are, call this  
  111.         // method again to print them  
  112.         if (root.HasChildNodes)  
  113.           RecurseXmlDocument(root.FirstChild, indent + 2);  
  114.  
  115.  
  116.         // Finally check to see if there are any siblings and if there are  
  117.         // call this method again to have them printed  
  118.         if (root.NextSibling != null)  
  119.           RecurseXmlDocument(root.NextSibling, indent);  
  120.       }  
  121.     }  
  122. //XPath選擇一個節點  
  123. //XPath語法相關參考http://www.w3school.com.cn/xpath/xpath_syntax.asp  
  124. private void buttonQueryNode_Click(object sender, EventArgs e)  
  125.         {  
  126.             // Load the XML document  
  127.             XmlDocument document = new XmlDocument();  
  128.             document.Load(@filePath);  
  129.  
  130.  
  131.             // Get the root element  
  132.             XmlElement root = document.DocumentElement;  
  133.  
  134.  
  135.             string queryStr = textBoxQueryText.Text;  
  136.  
  137.  
  138.             XmlNodeList nodeList = root.SelectNodes(queryStr);  
  139.             listBoxXmlNodes.Items.Clear();  
  140.  
  141.  
  142.             foreach (XmlNode n in nodeList)  
  143.             {  
  144.                 RecurseXmlDocument(n, 0);  
  145.             }  
  146.         } 

C#數據訪問XML的例子結束,希望對大家有用。

【編輯推薦】

  1. C#發送Email郵件的方法解析
  2. 解析C#中is和as操作符的用法
  3. C# Excel COM組件的使用
  4. 如何判斷C#字符串是全角還是半角
  5. C#語言規范之小結
責任編輯:book05 來源: 新浪博客
相關推薦

2009-07-22 17:15:04

C#實現

2009-08-18 17:19:33

C#事件模型

2009-07-30 18:18:27

C#時間計算

2010-06-28 09:53:11

SQL Server數

2009-08-19 14:15:42

C# 復合控件

2009-08-13 14:59:00

C#數據訪問層

2009-08-25 01:46:00

C# WINDOWS服

2024-11-08 09:44:44

數據庫C#數據源

2009-09-11 09:11:09

2009-09-04 18:00:54

C#數據訪問層

2009-08-31 14:19:20

C#打開一個文件

2009-08-25 15:23:16

C#子線程

2009-09-01 16:03:32

C#單元測試

2009-08-31 13:53:03

C#創建一個文件

2011-03-17 15:59:37

c#數據庫

2009-08-12 16:37:22

C#變量類型轉換

2009-07-14 16:02:42

JDBC例子

2014-04-15 13:01:58

FinallyC#

2013-04-03 10:22:00

iOS開發Objective-C

2009-07-31 17:14:19

C#語言Web程序
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产蜜臀97一区二区三区 | 欧美一级黄色免费看 | 黄色免费网站在线看 | 九色 在线| 日韩一区在线视频 | 国产精品欧美一区喷水 | 国产精品久久久久久久久久99 | 国产精品国产a | 日韩成人在线播放 | 成人自拍视频网站 | 久久久久久久久久久久亚洲 | 国产九九九九 | 在线免费观看黄a | 国产欧美日韩视频 | 欧美日韩激情 | 天天爽天天操 | 午夜视频一区 | 国产精品揄拍一区二区 | 精品中文字幕一区 | 精品免费在线 | 日本免费在线观看视频 | 精品美女在线观看 | 99精品电影 | 欧美一区二区三区 | 中文字幕 在线观看 | 91九色在线观看 | 一区二区三区国产精品 | 国产成人免费网站 | 日韩精品一区二区三区在线观看 | 久久久www成人免费精品 | 欧洲尺码日本国产精品 | 亚洲国产精品久久久 | 欧美日韩国产精品一区 | 99久久久无码国产精品 | 欧美黄色性生活视频 | 亚洲播放一区 | 性高朝久久久久久久3小时 av一区二区三区四区 | 欧美 日韩 亚洲91麻豆精品 | 精品视频久久久久久 | 久久久久久免费毛片精品 | 日韩精品在线看 |