新聞中心
在舉C#數(shù)據(jù)訪問(wèn)XML的例子之前,首先介紹一些知識(shí)和定義。

創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供廉江企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、HTML5、小程序制作等業(yè)務(wù)。10年已為廉江眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)的建站公司優(yōu)惠進(jìn)行中。
XML DOM的類(lèi)所在的命名空間為System.Xml中
XmlNode 表示文檔中的節(jié)點(diǎn),如果這個(gè)節(jié)點(diǎn)表示XML的文檔的根,就可以從它導(dǎo)航到文檔的任意位置
XmlDocument 常常作為使用XML的***個(gè)對(duì)象,這個(gè)類(lèi)用于加載和保存磁盤(pán)上或者其他位置的數(shù)據(jù)
XmlElement 表示XML文檔中的一個(gè)元素,派生于XmlLinkedNode,XmlLinkedNode派生于XmlNode
XmlAttribute 表示XMl的一個(gè)屬性
XmlText 表示開(kāi)標(biāo)記和閉標(biāo)記之間的文本內(nèi)容
XmlComment 表示一種特殊類(lèi)型的節(jié)點(diǎn),這種節(jié)點(diǎn)不是文檔的一部分,但是為讀者提供部分信息,通常是注釋
XmlNodeList 表示一個(gè)節(jié)點(diǎn)集合
C#數(shù)據(jù)訪問(wèn)XML示例:
XmlDocument document = new XmlDocument();
document.Loda(@"C:\Test\books.xml");
XmlElement element = document.DocumentElement;//返回一個(gè)XmlElement實(shí)例
示例1:
- //創(chuàng)建一個(gè)節(jié)點(diǎn)
- 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);
- }
- //刪除一個(gè)節(jié)點(diǎn)
- 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);
- }
- }
- //在一個(gè)ListBox中顯示文檔的所有節(jié)點(diǎn)名稱以及文本節(jié)點(diǎn)的內(nèi)容
- 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選擇一個(gè)節(jié)點(diǎn)
- //XPath語(yǔ)法相關(guān)參考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#數(shù)據(jù)訪問(wèn)XML的例子結(jié)束,希望對(duì)大家有用。
分享名稱:一個(gè)C#數(shù)據(jù)訪問(wèn)XML的例子
路徑分享:http://www.dlmjj.cn/article/dhcjegd.html


咨詢
建站咨詢
