iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >C#如何实现数据访问XML
  • 544
分享到

C#如何实现数据访问XML

2023-06-17 23:06:40 544人浏览 泡泡鱼
摘要

这篇文章给大家分享的是有关C#如何实现数据访问XML的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。在举C#数据访问XML的例子之前,首先介绍一些知识和定义。XML DOM的类所在的命名空间为System.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:

//创建一个节点  private void buttonCreateNode_Click(object sender, EventArgs e)          {              // Load the XML document              XmlDocument document = new XmlDocument();              document.Load("../../Books.xml");                // Get the root element              XmlElement root = document.DocumentElement;                // Create the new nodes              XmlElement newBook = document.CreateElement("book");              XmlElement newTitle = document.CreateElement("title");              XmlElement newAuthor = document.CreateElement("author");              XmlElement newCode = document.CreateElement("code");              XmlText title = document.CreateTextNode("Beginning Visual C# 3rd Edition");              XmlText author = document.CreateTextNode("Karli Watson et al");              XmlText code = document.CreateTextNode("1234567890");              XmlComment comment = document.CreateComment("This book is the book you are reading");                // Insert the elements              newBook.AppendChild(comment);              newBook.AppendChild(newTitle);              newBook.AppendChild(newAuthor);              newBook.AppendChild(newCode);              newTitle.AppendChild(title);              newAuthor.AppendChild(author);              newCode.AppendChild(code);              root.InsertAfter(newBook, root.LastChild);                document.Save("../../Books.xml");                listBoxXmlNodes.Items.Clear();              RecurseXmlDocument((XmlNode)document.DocumentElement, 0);          }  //删除一个节点  private void buttonDeleteNode_Click(object sender, EventArgs e)          {              // Load the XML document              XmlDocument document = new XmlDocument();              document.Load("../../Books.xml");                // Get the root element              XmlElement root = document.DocumentElement;                // Find the node. root is the < books> tag, so its last child which will be the              // last < book> node              if (root.HasChildNodes)              {                  XmlNode book = root.LastChild;                    // Delete the child                  root.RemoveChild(book);                    // Save the document back to disk                  document.Save("../../Books.xml");                  listBoxXmlNodes.Items.Clear();                    RecurseXmlDocument((XmlNode)document.DocumentElement, 0);              }          }  //在一个ListBox中显示文档的所有节点名称以及文本节点的内容  private void RecurseXmlDocument(XmlNode root, int indent)      {        // Make sure we don't do anything if the root is null        if (root == null)          return;          if (root is XmlElement) // Root is an XmlElement type        {          // first, print the name          listBoxXmlNodes.Items.Add(root.Name.PadLeft(root.Name.Length + indent));            // Then check if there are any child nodes and if there are, call this          // method again to print them          if (root.HasChildNodes)            RecurseXmlDocument(root.FirstChild, indent + 2);            // Finally check to see if there are any siblings and if there are          // call this method again to have them printed          if (root.NextSibling != null)            RecurseXmlDocument(root.NextSibling, indent);        }        else if (root is XmlText)        {          // Print the text          string text = ((XmlText)root).Value;          listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));        }        else if (root is XmlComment)        {          // Print text          string text = root.Value;          listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));            // Then check if there are any child nodes and if there are, call this          // method again to print them          if (root.HasChildNodes)            RecurseXmlDocument(root.FirstChild, indent + 2);            // Finally check to see if there are any siblings and if there are          // call this method again to have them printed          if (root.NextSibling != null)            RecurseXmlDocument(root.NextSibling, indent);        }      }  //XPath选择一个节点  //XPath语法相关参考Http://www.w3school.com.cn/xpath/xpath_syntax.asp  private void buttonQueryNode_Click(object sender, EventArgs e)          {              // Load the XML document              XmlDocument document = new XmlDocument();              document.Load(@filePath);                // Get the root element              XmlElement root = document.DocumentElement;                string queryStr = textBoxQueryText.Text;                XmlNodeList nodeList = root.SelectNodes(queryStr);              listBoxXmlNodes.Items.Clear();                foreach (XmlNode n in nodeList)              {                  RecurseXmlDocument(n, 0);              }          }

感谢各位的阅读!关于“C#如何实现数据访问XML”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

--结束END--

本文标题: C#如何实现数据访问XML

本文链接: https://www.lsjlt.com/news/294205.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C#如何实现数据访问XML
    这篇文章给大家分享的是有关C#如何实现数据访问XML的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。在举C#数据访问XML的例子之前,首先介绍一些知识和定义。XML DOM的类所在的命名空间为System.Xml中...
    99+
    2023-06-17
  • Ajax怎么跨域访问XML数据
    这篇文章主要介绍“Ajax怎么跨域访问XML数据”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Ajax怎么跨域访问XML数据”文章能帮助大家解决问题。 XML数据...
    99+
    2024-04-02
  • 织梦CMS如何实现数据库访问
    织梦CMS是一款基于PHP语言开发的开源内容管理系统,广泛应用于网站建设领域。在网站开发过程中,数据库的访问是一个非常重要的环节,它涉及到网站数据的存储、读取和更新等操作。接下来,我们...
    99+
    2024-03-13
    实现 数据库访问 织梦cms
  • 如何进行MySql数据库C++访问
    如何进行MySql数据库C++访问,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。 MySql数据库C++访问...
    99+
    2024-04-02
  • C#利用反射实现多数据库访问
    在上一篇文章中讲解了什么是反射,以及利用反射可以获取程序集里面的哪些内容。在平时的项目中,可能会遇到项目需要使用多种数据库,这篇文章中将会讲解如何利用反射实现访问多种数据库。 项目整...
    99+
    2024-04-02
  • 如何实现SQLite数据库访问与生成
    这篇文章将为大家详细讲解有关如何实现SQLite数据库访问与生成,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Xamarin数据库访问方式本节我们将讲解数据库的方式方式以及数据库的生成方法。访问方式Xam...
    99+
    2023-06-03
  • mysql+c语言+API如何访问数据库
    小编给大家分享一下mysql+c语言+API如何访问数据库,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!  #incl...
    99+
    2024-04-02
  • 如何使用c#访问存取SQL数据
    这篇文章主要介绍“如何使用c#访问存取SQL数据”,在日常操作中,相信很多人在如何使用c#访问存取SQL数据问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何使用c#访问存取...
    99+
    2024-04-02
  • Spring Boot使用MyBatis如何实现访问数据库
    今天就跟大家聊聊有关Spring Boot使用MyBatis如何实现访问数据库,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。基于spring boot开发的微服务应用,与MyBati...
    99+
    2023-05-31
    springboot 数据库 mybatis
  • C#如何实现XML序列化
    这篇文章给大家分享的是有关C#如何实现XML序列化的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。实现C# XML序列化技术使用到什么具体的方法呢?我们在具体的操作过程中需要注意什么呢?那么这里向你展示一个Demo...
    99+
    2023-06-17
  • C#使用ADO.Net连接数据库与DbProviderFactory实现多数据库访问
    一、ADO.Net数据库连接字符串 1、OdbcConnection(System.Data.Odbc) (1)SQL Sever 标准安全:" Driver={SQL S...
    99+
    2024-04-02
  • ADO.NET如何实体数据访问和更改
    本篇内容介绍了“ADO.NET如何实体数据访问和更改”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在向大家详细介绍ADO.NET实体数据之前...
    99+
    2023-06-17
  • C#怎么使用ADO.Net连接数据库与实现多数据库访问
    本文小编为大家详细介绍“C#怎么使用ADO.Net连接数据库与实现多数据库访问”,内容详细,步骤清晰,细节处理妥当,希望这篇“C#怎么使用ADO.Net连接数据库与实现多数据库访问”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来...
    99+
    2023-06-30
  • java如何实现https访问
    利用java实现https访问,具体方法如下:String result = "";URL url = new URL("网址");HttpsURLConnection conn = (HttpsURLConnection)url.open...
    99+
    2024-04-02
  • 外网如何访问数据库
    小编给大家分享一下外网如何访问数据库,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧! 外网访问数据库的...
    99+
    2024-04-02
  • c#怎么访问mysql数据库
    要访问MySQL数据库,可以使用MySQL官方提供的MySQL Connector/Net库。以下是使用C#连接MySQL数据库的一...
    99+
    2024-04-18
    c# mysql
  • c++怎么访问mysql数据库
    在C++中访问MySQL数据库通常需要使用MySQL官方提供的MySQL C API。以下是一些基本步骤来访问MySQL数据库: ...
    99+
    2024-04-18
    c++ mysql
  • php如何实现访问量
    小编给大家分享一下php如何实现访问量,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!php实现访问量的方法:1、读出数据文件;2、把累加后的数据写入文件“coun...
    99+
    2023-06-22
  • Holer实现外网访问PostgreSQL数据库
    外网访问内网PostgreSQL数据库 内网主机上安装了PostgreSQL数据库,只能在局域网内访问,怎样从公网也能访问本地PostgreSQL数据库? 本文将介绍使用holer实现的具体步骤。...
    99+
    2024-04-02
  • Holer实现MongoDB数据库外网访问
    外网访问内网MongoDB数据库 内网主机上安装了MongoDB数据库,只能在局域网内访问,怎样从公网也能访问本地MongoDB数据库? 本文将介绍使用holer实现的具体步骤。 1. 准备工...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作