C # code operation XML (add, delete, modify ),
Directory:
XML, a small data storage and transmission tool, is certainly not unfamiliar to everyone. Today we will summarize some simple XML operations.
These operations are all performed on the console ·····
1. Create XML
1) create common XML
Static void Main (string [] args) {// use code to create an XML document // 1. Reference The namespace // 2. Create an XML Document Object XmlDocument doc = new XmlDocument (); // 3. Create the first line of description and add it to the doc file 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 the document doc. appendChild (books); // 5. Create a subnode XmlElement book1 = doc for the root node Books. createElement ("Book"); // Add the book to the root node books. appendChild (book1); // 6. Add the sub-node XmlElement name1 = doc to book1. createElement ("Name"); name1.InnerText = "Romance of the Three Kingdoms"; book1.AppendChild (name1); XmlElement price1 = doc. createElement ("Price"); price1.InnerText = "70"; book1.AppendChild (price1); XmlElement des1 = doc. createElement ("Des"); des1.InnerText = ""; book1.AppendChild (des1); XmlElement book2 = doc. createElement ("Book"); books. appendChild (book2); XmlElement name2 = doc. createElement ("Name"); name2.InnerText = "Journey to the West"; book2.AppendChild (name2); XmlElement price2 = doc. createElement ("Price"); price2.InnerText = "80"; book2.AppendChild (price2); XmlElement des2 = doc. createElement ("Des"); des2.InnerText = "not bad"; book2.AppendChild (des2); doc. save ("Books. xml "); Console. writeLine ("saved successfully"); Console. readKey ();}View Code
According to the code and run it, we will get the XML document we want:
2) create 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 = "Zhang San"; 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 orderItem1.SetAttribute ("Name", "SLR") to the node; orderItem1.SetAttribute ("Count", "1120"); items. appendChild (orderItem1); XmlElement orderItem2 = doc. createElement ("OrderItem"); // Add the orderItem2.SetAttribute ("Name", "") to the node; orderItem2.SetAttribute ("Count", "30"); items. appendChild (orderItem2); XmlElement orderItem3 = doc. createElement ("OrderItem"); // Add orderItem3.SetAttribute ("Name", "mobile") to the node; orderItem3.SetAttribute ("Count", "2000"); items. appendChild (orderItem3); doc. save ("Order. xml "); Console. writeLine ("saved successfully"); Console. readKey ();}View Code
According to the code and run it, we will get the XML document we want:
2. append XML
Static void Main (string [] args) {// append the XML document XmlDocument doc = new XmlDocument (); XmlElement books; if (File. exists ("Books. xml ") {// if the file already exists, load the XML doc. load ("Books. xml "); // get the file's root node 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 and node books = doc. createElement ("Books"); doc. appendChild (books);} // 5. Create a subnode XmlElement book1 = doc for the root node Books. createElement ("Book"); // Add the book to the root node books. appendChild (book1); // 6. Add the sub-node XmlElement name1 = doc to book1. createElement ("Name"); name1.InnerText = "c # "; book1.AppendChild (name1); XmlElement price1 = doc. createElement ("Price"); price1.InnerText = "110"; book1.AppendChild (price1); XmlElement des1 = doc. createElement ("Des"); des1.InnerText = "do not understand"; book1.AppendChild (des1); doc. save ("Books. xml "); Console. writeLine ("saved successfully"); Console. readKey ();}View Code
According to the code and run it, we will get the XML document we want:
3. Read XML
1) read common XML
Static void Main (string [] args) {XmlDocument doc = new XmlDocument (); // load the XML doc to be read. load ("Books. xml "); // get the root node XmlElement books = doc. documentElement; // get the collection of returned nodes from the subnode XmlNodeList xnl = books. childNodes; foreach (XmlNode item in xnl) {Console. writeLine (item. innerText);} Console. readKey ();}View Code
After the code is written and run, the read XML result is obtained:
2) read XML with attributes
Static void Main (string [] args) {// read the XML document with attributes 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 ();}View Code
After the code is written and run, the read XML result is obtained:
4. Modify the attribute value
Static void Main (string [] args) {// change the attribute value XmlDocument doc = new XmlDocument (); doc. load ("Order. xml "); XmlNode xn = doc. selectSingleNode ("/Order/Items/OrderItem [@ Name = 'sls']"); xn. attributes ["Count"]. value = "2000"; xn. attributes ["Name"]. value = "computer"; doc. save ("Order. xml "); Console. writeLine ("saved successfully"); Console. readKey ();}View Code
After the code is written and run, the modified XML result is obtained:
5. delete an XML Node
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 ("deleted successfully"); Console. readKey ();}View Code
After the code is written and run, the XML result after the deletion is obtained:
So far: the simple addition, deletion, and modification operations of XML have ended ·····