一个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 来源: 新浪博客
相关推荐

2010-06-28 09:53:11

SQL Server数

2009-07-30 18:18:27

C#时间计算

2009-08-18 17:19:33

C#事件模型

2009-07-22 17:15:04

C#实现

2009-08-19 14:15:42

C# 复合控件

2009-08-13 14:59:00

C#数据访问层

2009-08-25 01:46:00

C# WINDOWS服

2009-09-04 18:00:54

C#数据访问层

2009-09-11 09:11:09

2009-08-31 14:19:20

C#打开一个文件

2011-03-17 15:59:37

c#数据库

2009-08-31 13:53:03

C#创建一个文件

2009-09-01 16:03:32

C#单元测试

2009-08-25 15:23:16

C#子线程

2014-04-15 13:01:58

FinallyC#

2013-04-03 10:22:00

iOS开发Objective-C

2009-08-12 16:37:22

C#变量类型转换

2009-07-14 16:02:42

JDBC例子

2009-07-31 17:14:19

C#语言Web程序

2013-02-25 10:18:08

ThreadMsgC#
点赞
收藏

51CTO技术栈公众号