温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#如何实现数据访问XML

发布时间:2021-12-03 10:06:47 来源:亿速云 阅读:172 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关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”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节
推荐阅读:
  1. c# 操作 xml
  2. C# create xml

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

xml
AI