您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)C#如何實(shí)現(xiàn)數(shù)據(jù)訪問XML的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
在舉C#數(shù)據(jù)訪問XML的例子之前,首先介紹一些知識和定義。
XML DOM的類所在的命名空間為System.Xml中
XmlNode 表示文檔中的節(jié)點(diǎn),如果這個(gè)節(jié)點(diǎn)表示XML的文檔的根,就可以從它導(dǎo)航到文檔的任意位置
XmlDocument 常常作為使用XML的***個(gè)對象,這個(gè)類用于加載和保存磁盤上或者其他位置的數(shù)據(jù)
XmlElement 表示XML文檔中的一個(gè)元素,派生于XmlLinkedNode,XmlLinkedNode派生于XmlNode
XmlAttribute 表示XMl的一個(gè)屬性
XmlText 表示開標(biāo)記和閉標(biāo)記之間的文本內(nèi)容
XmlComment 表示一種特殊類型的節(jié)點(diǎn),這種節(jié)點(diǎn)不是文檔的一部分,但是為讀者提供部分信息,通常是注釋
XmlNodeList 表示一個(gè)節(jié)點(diǎn)集合
C#數(shù)據(jù)訪問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語法相關(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); } }
感謝各位的閱讀!關(guān)于“C#如何實(shí)現(xiàn)數(shù)據(jù)訪問XML”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。