C#如何实现数据访问XML
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,这篇文章给大家分享的是有关C#如何实现数据访问XML的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。在举C#数据访问XML的例子之前,首先介绍一些知识和定义。XML DOM的
千家信息网最后更新 2025年11月14日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安全错误
数据库的锁怎样保障安全
实用网络技术学习
联想企业服务器
网络安全和经济事件
正规网络技术图片
tbox与服务器通讯
联想乐云服务器无法连接
服务器防护哪家比较专业
江门台山网络安全
榕基软件 网络安全
毕业后做什么软件开发
兴化网络技术参考价格
网络技术维护的日常工作
python图形软件开发
南方电网软件开发笔试多久出结果
网络安全协议心得
南华大学网络安全宣传周
单位网络安全要求会议
香港服务器99idc
关于网络安全的班团课
验证数据库连接
控制台连接数据库
趋势杀毒 网络安全版
数据库视图的局限性
数据库mongo db
南昌app软件开发费用多少
数据库启动错误怎么办
vrd网络技术
WCDMA网络安全黑板报
2018网络技术应用试卷1
江苏义迅网络技术