自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一個C#數(shù)據(jù)訪問XML的例子

開發(fā) 后端
本文舉例說明了C#數(shù)據(jù)訪問XML的方法,希望大家從這個例子中學(xué)到相關(guān)的知識。

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

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

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

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

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

XmlAttribute 表示XMl的一個屬性

XmlText 表示開標記和閉標記之間的文本內(nèi)容

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

XmlNodeList 表示一個節(jié)點集合

C#數(shù)據(jù)訪問XML示例:

XmlDocument document = new XmlDocument();

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

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

示例1:

  1. //創(chuàng)建一個節(jié)點  
  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. //刪除一個節(jié)點  
  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中顯示文檔的所有節(jié)點名稱以及文本節(jié)點的內(nèi)容  
  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選擇一個節(jié)點  
  123. //XPath語法相關(guān)參考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#數(shù)據(jù)訪問XML的例子結(jié)束,希望對大家有用。

【編輯推薦】

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

2010-06-28 09:53:11

SQL Server數(shù)

2009-07-30 18:18:27

C#時間計算

2009-08-18 17:19:33

C#事件模型

2009-07-22 17:15:04

C#實現(xiàn)

2009-08-19 14:15:42

C# 復(fù)合控件

2024-11-08 09:44:44

數(shù)據(jù)庫C#數(shù)據(jù)源

2009-08-13 14:59:00

C#數(shù)據(jù)訪問層

2009-08-25 01:46:00

C# WINDOWS服

2009-09-04 18:00:54

C#數(shù)據(jù)訪問層

2009-09-11 09:11:09

2009-08-31 14:19:20

C#打開一個文件

2011-03-17 15:59:37

c#數(shù)據(jù)庫

2009-08-25 15:23:16

C#子線程

2009-09-01 16:03:32

C#單元測試

2009-08-31 13:53:03

C#創(chuàng)建一個文件

2009-08-12 16:37:22

C#變量類型轉(zhuǎn)換

2014-04-15 13:01:58

FinallyC#

2013-04-03 10:22:00

iOS開發(fā)Objective-C

2009-07-14 16:02:42

JDBC例子

2009-07-31 17:14:19

C#語言Web程序
點贊
收藏

51CTO技術(shù)棧公眾號