C # Operations XML file Instance Rollup

Source: Internet
Author: User

Manipulation of XML files is a very common feature in C # programming. This article shows a few common examples of C # operations XML files as an example. Specific as follows:


1. Return to the node subscript

public static XmlDocument Getdoc (String path)//load XML document {  XmlDocument doc = new XmlDocument ();  Doc. Load (path);  return doc;} <summary>///returns the found node subscript///</summary>///<param name= "path" >xml file path </param>///<param Name= "Bname" > title </param>///<returns></returns>public static int getPosition (String path,string node, String bname) {  int i;  XmlDocument doc = new XmlDocument ();  Doc. Load (path);  XmlNodeList nodeList = doc. selectSingleNode (node). ChildNodes;  for (i = 0; i < Nodelist.count; i++)  {if (Nodelist[i]. Childnodes[0]. InnerText = = bname) {   return i;}  }  return-1;}


2. Traversing data

<summary>///traversal data///</summary>///<param name= "sender" ></param>///<param name= "E" ></param>protected void Btnread_click (object sender, EventArgs e) {  XmlDocument doc = Getdoc ("books.xml") ;//load XML document, books file is stored in  foreach (XmlElement root in Doc. Documentelement.childnodes)//traverse the root of the child node  {Response.Write ("<script>document.write (' <br/> '); </ Script> "); Response.Write ("<script>document.write" + root. Name + "');</script>");//node name root. Name book foreach (XmlElement item in root. ChildNodes) {   Response.Write ("<script>document.write (') + item. Name + "' + ': ' + ' + item. InnerText + "');</script>");//Output node name and text node value   Response.Write ("<script>document.write (' <br/> ') ;</script> "); }  }}


3. Find

<summary>///Find//</summary>///<param name= "path" > File path </param>///<param name= "Node" > Node </param>///<param name= "bname" > Find keyword </param>///<returns>XmlNode</returns> public static XmlNode Find (string path,string node,string bname) {  XmlDocument doc = new XmlDocument ();  Doc. Load (path);//Load XML document  XmlNodeList NodeList = doc. selectSingleNode (node). ChildNodes;  int i = getPosition (path, node, bname);/  if (I >= 0) return nodelist[i];  else return null;}


4. Delete a node

<summary>///delete element, attribute///</summary>///<param name= "path" > File </param>///<param name= " Node > The parent node of the specified node </param>///<param name= "attribute" > delete the node when it is empty, otherwise delete the property </param>///<param name= " Bname "></param>public static void Delete (String path,string node,string attribute,string bname) {  XmlDocument doc = new XmlDocument ();  Doc. Load (path);  XmlNode root = Doc. selectSingleNode (node);  XmlNodeList nodeList = doc. selectSingleNode (node). ChildNodes;  int i = getPosition (path, node, bname);//returns the specified node subscript  if (i >= 0)  {if (attribute. Equals ("")) {   root. RemoveChild (Nodelist[i]); } else {   XmlElement xn = (XmlElement) nodelist[i];   Xn. RemoveAttribute (attribute); }  }  doc. Save (path);


5. Add

<summary>///add element value///</summary>///<param name= "path" ></param>///<param name= "Node" ></param>///<param name= "element" ></param>///<param name= "value" ></param>/// <param name= "I" > Insert subscript, if negative, default from last node insert </param>///<returns></returns>public static bool Add (String path,string node,string element,string value,int i) {  XmlDocument doc = new XmlDocument ();  Doc. Load (path);  XmlNodeList nodeList = doc. selectnodes (node);  XmlNode NewNode = doc. selectSingleNode (node). LastChild;  if (I < 0 | | i > NODELIST.COUNT-1)//If it is less than 0 or greater than the node length, the default is to add  {XmlElement newelement = doc from the last node. createelement (element);//create elements Newelement.innertext = value;//assignment Newnode.appendchild (newelement);  }  else  {XmlElement newelement = doc. createelement (Element); Newelement.innertext = value; NODELIST[I-1]. AppendChild (newelement);  }  Doc. Save (path);  return true;}

It is hoped that the method described in this article will be helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Operations XML file Instance Rollup

This address: http://www.paobuke.com/develop/c-develop/pbk23627.html






Related content C # calls Python files to perform ASP. NET core MVC Permissions control: Control operation rights in view C # verification code creation and use example C # use time and then string to create a unique order number
In-depth parsing of jagged arrays and implicitly typed arrays in C # How to open and read a USB file directory example of a simple boxed operation in C # using C # to get the HTML source of a Web page

C # Operations XML file Instance Rollup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.