.net how to operate xml
This article mainly introduces how to operate xml. Net has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
The code is as follows:
Using System.Xml;// initializes a xml instance XmlDocument xml=new XmlDocument (); / / imports the specified xml file xml.Load (path); xml.Load (HttpContext.Current.Server.MapPath ("~ / file/bookstore.xml")); / / specifies a node XmlNode root=xml.SelectSingleNode ("/ root"); / / gets all the direct child nodes under the node XmlNodeList childlist=root.ChildNodes;// to determine whether there is a child node root.HasChildNodes under that node / / get a collection of peer nodes with the same name XmlNodeList nodelist=xml.SelectNodes ("/ Root/News"); / / generate a new node XmlElement node=xml.CreateElement ("News"); / / add a node to the specified node as its child node root.AppendChild (node); / / add the node to a child node under the specified node before root.InsertBefore (node,root.ChildeNodes [I]); / / assign the new attribute of the specified node and assign the value node.SetAttribute ("id", "11111") / / add a child node root.AppendChild (node) for the specified node; / / get the specified attribute value of the specified node string id=node.Attributes ["id"] .Value;// gets the text string content=node.InnerText;// in the specified node to save the XML file string path=Server.MapPath ("~ / file/bookstore.xml"); xml.Save (path); / / or use: xml.Save (HttpContext.Current.Server.MapPath ("~ / file/bookstore.xml")) Thank you for reading this article carefully. I hope the article ".net how to operate xml" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!