一个C 一个炒熟的番茄
创始人
2024-05-05 05:00:45
0

在举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#语言规范之小结

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...