C#如何实现数据访问XML
发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,这篇文章给大家分享的是有关C#如何实现数据访问XML的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。在举C#数据访问XML的例子之前,首先介绍一些知识和定义。XML DOM的
千家信息网最后更新 2025年02月07日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:
//创建一个节点 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"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
节点
文档
数据
C#
内容
位置
文本
更多
标记
知识
示例
篇文章
参考
不错
实用
特殊
之间
例子
信息
元素
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
血猎玩什么服务器
福州天辉网络技术有限公司
软件开发设计与制造专业排名
网络安全课件ppt百度
acess如何下载数据库
投资软件开发注意事项
安卓手机网络安全认证
茂名卫星软件开发厂家直销
网络技术专业报考学校
共享电瓶车服务器
内燃机车视频软件开发
网络安全 画像
购买免流服务器网址
数据库项目技术总结
数据库系统概论题集百度云
温州分布式服务器行情
中睿盛通软件开发
宣传防范网络安全
自动化软件开发是什么
网络安全还属于热门行业吗
nginx多台服务器
饥荒联机版为什么服务器刷新很慢
怎么做好网络安全宣传教育
我的世界国际基岩版怎么开服务器
软件开发初创公司成本
阿拉德之怒自己开服务器
ogg支持梦达数据库吗
mysql锁定多行数据库
广州无限软件开发供应商
网络安全还属于热门行业吗