在举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的例子结束,希望对大家有用。
【编辑推荐】