In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how C # implements data access to XML. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Before giving an example of C # data access XML, let's introduce some knowledge and definitions.
The class of XML DOM is in the namespace of System.Xml
XmlNode represents a node in a document, and if this node represents the root of a XML document, you can navigate from it to anywhere in the document
XmlDocument is often used as a * object using XML, a class used to load and save data on disk or elsewhere
XmlElement represents an element in an XML document, derived from XmlLinkedNode,XmlLinkedNode and derived from XmlNode
XmlAttribute represents an attribute of XMl
XmlText represents the text content between the open and closed tags
XmlComment represents a special type of node that is not part of the document but provides the reader with some information, usually comments
XmlNodeList represents a collection of nodes
C# data access XML example:
XmlDocument document = new XmlDocument ()
Document.Loda (@ "C:\ Test\ books.xml")
XmlElement element = document.DocumentElement;// returns a XmlElement instance
Example 1:
/ / create a node 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);} / / delete a node 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);}} / / displays all the node names of the document and the content of the text node private void RecurseXmlDocument (XmlNode root, int indent) {/ / Make sure we don't do anything if the root is null if (root = = null) return in one ListBox 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 Select a node / / XPath syntax related references 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) Thank you for your reading! This is the end of the article on "how to achieve data access XML in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.