Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of XmlDocument object Operation

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of XmlDocument object operation, the article is very detailed, has a certain reference value, interested friends must read it!

It is convenient to use XmlReader to traverse documents, and it is easy to generate a new XML document using XmlWriter. But it is troublesome to modify an existing XML, such as adding an element or changing an attribute value. At this point, you can use the XmlDocument object to modify the document by calling the DOM method, and then save it. Because DOM has been standardized and supported by many languages, such as JS, many of the methods here are consistent with those in JS, such as GetElementByID (), GetElementByTagName (), AppendChild (), InsertAfter (), etc.

1. Create a xml file

XmlDocument doc = new XmlDocument (); XmlDeclaration dec = doc.CreateXmlDeclaration ("1.0", "UTF-8", null); doc.AppendChild (dec); XmlElement root = doc.CreateElement ("bookstore"); / / create root node doc.AppendChild (root); XmlElement newBook = _ doc.CreateElement ("book"); / / create a new 'book' element//set some attributesnewBook.SetAttribute ("genre", "Mystery"); newBook.SetAttribute ("publicationdate", "2001") NewBook.SetAttribute ("ISBN", "123456789"); / / create a new 'title' elementXmlElement newTitle = _ doc.CreateElement ("title"); newTitle.InnerText = "Case of the Missing Cookie"; newBook.AppendChild (newTitle); / / create new author elementXmlElement newAuthor = _ doc.CreateElement ("author"); newBook.AppendChild (newAuthor); / / create new name elementXmlElement newName = _ doc.CreateElement ("name"); newName.InnerText = "Cookie Monster" NewAuthor.AppendChild (newName); / / create new price element XmlElement newPrice = _ doc.CreateElement ("price"); newPrice.InnerText = "9.95"; newBook.AppendChild (newPrice); / / add to the current documentdoc.DocumentElement.AppendChild (newBook); / / _ doc.DocumentElement is the root doc.Save that gets the xml ("bb.xml"); saves the XML document to the specified location

2. Inserting a node is similar to creating a xml file

_ doc.Load ("books.xml"); XmlElement newBook = _ doc.CreateElement ("book"); newBook.SetAttribute ("genre", "Mystery"); newBook.SetAttribute ("publicationdate", "2001"); newBook.SetAttribute ("ISBN", "123456789"); XmlElement newTitle = _ doc.CreateElement ("title"); newTitle.InnerText = "Case of the Missing Cookie"; newBook.AppendChild (newTitle) XmlElement newAuthor = _ doc.CreateElement ("author"); newBook.AppendChild (newAuthor); XmlElement newName = _ doc.CreateElement ("name"); newName.InnerText = "Cookie Monster"; newAuthor.AppendChild (newName); XmlElement newPrice = _ doc.CreateElement ("price"); newPrice.InnerText = "9.95"; newBook.AppendChild (newPrice); _ doc.DocumentElement.AppendChild (newBook); _ doc.Save ("booksEdit.xml") Or save XmlTextWriter tr = new XmlTextWriter ("booksEdit.xml", null) as follows; / / Save the xml document or, if it exists, override tr.Formatting = Formatting.Indented; _ doc.WriteContentTo (tr); tr.Close ()

3. Modify the xml node

Change the genre value of the node with the value of "novel" to "updatenovel", and change the text of the child nodes of that node to "la".

XmlNodeList nodeList=xmlDoc.SelectSingleNode ("bookstore"). ChildNodes;// gets all child nodes of bookstore node foreach (XmlNode xn in nodeList) / / traverses all child nodes {XmlElement xe= (XmlElement) xn;// converts the child node type to XmlElement type if (xe.GetAttribute ("genre") = = "novel") / / if the genre attribute value is "Li Zanhong" {xe.SetAttribute ("genre", "updatenovel"); / / change the attribute to "update Li Zanhong" XmlNodeList nls=xe.ChildNodes / / continue to get all the child nodes of the Xe child node foreach (XmlNode xn1 in nls) / / traverse {XmlElement xe2= (XmlElement) xn1;// transformation type if (xe2.Name== "title") / / if {xe2.InnerText= "Yasheng" is found; / / then modify the break;// and find the exit} break;}} xmlDoc.Save ("bookstore.xml"); / / save.

4. Delete a node

The genre attribute of the node, delete the node.

XmlNodeList xnl=xmlDoc.SelectSingleNode ("bookstore") .ChildNodes; foreach (XmlNode xn in xnl) {XmlElement xe= (XmlElement) xn; if (xe.GetAttribute ("genre") = = "fantasy") {xe.RemoveAttribute ("genre"); / / Delete genre attribute} else if (xe.GetAttribute ("genre") = = "update Li Zanhong") {xe.RemoveAll (); / / Delete all contents of this node}} xmlDoc.Save ("bookstore.xml")

5. Traverse xml

String filePath = "bookstore.xml"; XmlDocument doc = new XmlDocument (); doc.Load (filePath); XmlNode root = doc.DocumentElement; showNode (root); private static void showNode (XmlNode root) {if (root.NodeType==XmlNodeType.Text) {Console.WriteLine (root.Value);} if (root.NodeType==XmlNodeType.Element) {Console.WriteLine (root.Name) } if (root.Attributes.Count > 0) {foreach (XmlAttribute attr in root.Attributes) {Console.Write ("{0} = {1}", attr.Name,attr.Value);} Console.WriteLine ();} XmlNodeList chiledList = root.ChildNodes; foreach (XmlNode child in chiledList) {showNode (child) }} above is all the content of the article "sample Analysis of XmlDocument object manipulation". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report