C # code operation XML to add, delete, change operation _c# Tutorial

Source: Internet
Author: User

As a small data storage delivery tool--xml, we are certainly not unfamiliar, today on the XML some simple operation to do some summary.
These are all operating on the console

1. Create XML
1) Create normal XML

static void Main (string[] args) {//creates an XML document//1, references a namespace//2, creates an XML Document object XmlDocument doc = new XmlDocument () by code; 3, create the first line description information, and add to Doc document XmlDeclaration Dec = doc.
  Createxmldeclaration ("1.0", "Utf-8", null); Doc.
  AppendChild (DEC); 4, create the root node XmlElement books = Doc.
  CreateElement ("books"); Add the root node to doc in the document.

  AppendChild (books); 5, to the root node books to create child nodes XmlElement Book1 = doc.
  CreateElement ("book"); Add book to root node books.
  AppendChild (BOOK1); 6, add child nodes to Book1 xmlelement name1 = doc.
  CreateElement ("Name"); Name1.
  InnerText = "The Most of the Kingdoms"; Book1.

  AppendChild (NAME1); XmlElement Price1 = doc.
  CreateElement ("Price"); Price1.
  innertext = "70"; Book1.

  AppendChild (Price1); XmlElement des1 = doc.
  CreateElement ("Des"); Des1.
  InnerText = "good-looking"; Book1.

  AppendChild (DES1); XmlElement book2 = doc.
  CreateElement ("book"); Books.


  AppendChild (BOOK2); XmlElement name2 = doc.
  CreateElement ("Name"); Name2.
  InnerText = "Journey to the Westward"; Book2.

  AppendChild (name2); XmlElement price2= Doc. CreAteelement ("Price"); Price2.
  innertext = "80"; Book2.

  AppendChild (PRICE2); XmlElement des2 = doc.
  CreateElement ("Des"); Des2.
  InnerText = "Still good"; Book2.

  AppendChild (Des2); Doc.
  Save ("books.xml");
  Console.WriteLine ("Save Success");
 Console.readkey ();

 }

Written according to the code, and then run, you'll get the XML document we want:

2 Creating XML with Attributes

static void Main (string[] args) {XmlDocument doc = new XmlDocument (); XmlDeclaration Dec = doc.
  Createxmldeclaration ("1.0", "Utf-8", "yes"); Doc.

  AppendChild (DEC); XmlElement order = doc.
  CreateElement ("Order"); Doc.

  AppendChild (order); XmlElement customerName = doc.
  CreateElement ("CustomerName");
  Customername.innertext = "John"; Order.

  AppendChild (CustomerName); XmlElement customernumber = doc.
  CreateElement ("CustomerNumber");
  Customernumber.innertext = "1010101"; Order.


  AppendChild (CustomerNumber); XmlElement items = doc.
  CreateElement ("Items"); Order.

  AppendChild (items); XmlElement orderItem1 = doc.
  CreateElement ("OrderItem");
  Add attribute Orderitem1.setattribute ("Name", "SLR") to the node;
  Orderitem1.setattribute ("Count", "1120"); Items.

  AppendChild (ORDERITEM1); XmlElement orderItem2 = doc.
  CreateElement ("OrderItem");
  Add attribute Orderitem2.setattribute ("Name", "book") to the node;
  Orderitem2.setattribute ("Count", "30"); Items.

  AppendChild (ORDERITEM2); XmlElement OrderItem3 = doc.
  CreateElement ("OrderItem");
  Add attribute Orderitem3.setattribute ("Name", "mobile") to the node;
  Orderitem3.setattribute ("Count", "2000"); Items.

  AppendChild (ORDERITEM3); Doc.
  Save ("Order.xml");
  Console.WriteLine ("Save Success");

  
 Console.readkey ();

 }

Written according to the code, and then run, you'll get the XML document we want:

2. Append XML

static void Main (string[] args) {//Append XML document XmlDocument doc = new XmlDocument ();
  XmlElement books; if (file.exists ("books.xml")) {//If the file exists to load XML doc.
  Load ("books.xml"); Gets the root node of the file books = doc.
  DocumentElement; else {//If the file does not exist//create the first line XmlDeclaration Dec = doc.
  Createxmldeclaration ("1.0", "Utf-8", null); Doc.
  AppendChild (DEC); Create with node books = doc.
  CreateElement ("books"); Doc.
  AppendChild (books); //5, create a child node for the root node books XmlElement Book1 = doc.
  CreateElement ("book"); Add book to root node books.


  AppendChild (BOOK1); 6, add child nodes to Book1 xmlelement name1 = doc.
  CreateElement ("Name"); Name1.
  innertext = "C # development Encyclopedia"; Book1.

  AppendChild (NAME1); XmlElement Price1 = doc.
  CreateElement ("Price"); Price1.
  InnerText = "110"; Book1.

  AppendChild (Price1); XmlElement des1 = doc.
  CreateElement ("Des"); Des1.
  InnerText = "Can not understand"; Book1.


  AppendChild (DES1); Doc.
  Save ("books.xml");
  Console.WriteLine ("Save Success");

 Console.readkey (); }

Written according to the code, and then run, you'll get the XML document we want:

3. Reading XML
1 reading normal XML

static void Main (string[] args)
 {
  XmlDocument doc = new XmlDocument ();
  Loads the XML doc to read
  . Load ("books.xml");

  Get root node
  xmlelement books = doc. documentelement;

  Gets the collection of child node return nodes
  xmlnodelist xnl = books. ChildNodes;

  foreach (XmlNode item in XNL)
  {
  Console.WriteLine (item. innertext);
  }
  Console.readkey ();
}

Written according to the code, and then run, you get the XML result read:

2 read XML with Attributes

 static void Main (string[] args)
 {
  //Read XML document with attribute

  xmldocument doc = new XmlDocument ();
  Doc. Load ("Order.xml");
  XmlNodeList xnl = doc. SelectNodes ("/order/items/orderitem");
  foreach (XmlNode node in xnl)
  {
  Console.WriteLine node. attributes["Name"]. Value);
  Console.WriteLine (node. attributes["Count"]. Value);
  }
  Console.readkey ();
}

Written according to the code, and then run, you get the XML result read:

4. Modify the value of the property

static void Main (string[] args)
 {
  //change the value of the property
  XmlDocument doc = new XmlDocument ();
  Doc. Load ("Order.xml");
  XmlNode xn = doc. selectSingleNode ("/order/items/orderitem[@Name = ' SLR ']");
  Xn. attributes["Count"]. Value = "n";
  Xn. attributes["Name"]. Value = "Computer";
  Doc. Save ("Order.xml");
  Console.WriteLine ("Save Success");

  Console.readkey ();
 }

Written according to the code, and then run, you will get the modified XML result:

5. Delete XML nodes

 static void Main (string[] args)
 {
  XmlDocument doc = new XmlDocument ();
  Doc. Load ("Order.xml");
  XmlNode xn = doc. selectSingleNode ("/order/items");
  Xn. RemoveAll ();
  Doc. Save ("Order.xml");
  Console.WriteLine ("Delete succeeded");
  Console.readkey ();
 }

Written according to the code, and then run, you will get the modified XML results after the deletion:

So far: XML simple additions and deletions to the end of the operation.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.