Simple additions, deletions, modifications, and viewing of XML files.
XML code:
<?XML version= "1.0" encoding= "UTF-8"?><Books> < Book> <name>Harry potter</name> < Price>10</ Price> <Memo>This is a good-looking book.</Memo> </ Book> < BookID= "B02"> <name>Three kingdoms</name> < Price>10</ Price> <Memo>One of the four major masterpieces.</Memo> </ Book> < BookID= "B03"> <name>Water Margin</name> < Price>6</ Price> <Memo>One of the four major masterpieces.</Memo> </ Book> < BookID= "B04"> <name>Red House</name> < Price>5</ Price> <Memo>One of the four major masterpieces.</Memo> </ Book></Books>
C # code:
usingSystem;usingSystem.Xml;namespacexmldemo{classProgram {Static voidMain (string[] args) {XmlElement Thebook=NULL, Theelem =NULL, root =NULL; XmlDocument xmldoc=NewXmlDocument (); Try{xmldoc. Load (@"C:\Users\HF_Ultrastrong\Desktop\WFA_Test\XMLDemo\Books.xml"); Root=xmldoc. DocumentElement; //---Start a new book----Thebook = xmldoc. CreateElement (" Book"); Theelem= xmldoc. CreateElement ("name"); Theelem.innertext="new book"; Thebook.appendchild (Theelem); Theelem= xmldoc. CreateElement (" Price"); Theelem.innertext=" -"; Thebook.appendchild (Theelem); Theelem= xmldoc. CreateElement ("Memo"); Theelem.innertext="The new book looks better. "; Thebook.appendchild (Theelem); Root. AppendChild (Thebook); Console.Out.WriteLine ("---Start a new book----"); Console.Out.WriteLine (Root. OuterXml); //---Create a new book to complete----//---The following to make some changes to the "Harry Potter". ---- //---Search for "Harry Potter"----Thebook = (XmlElement) root. selectSingleNode ("/books/book[name= ' Harry Potter '"); Console.Out.WriteLine ("---Find Harry Potter----"); Console.Out.WriteLine (Thebook.outerxml); //---Revise the price of the book at this time-----//getElementsByTagName returns the nodelist, so keep the item (0). In addition, getElementsByTagName ("price") is equivalent to selectnodes (".//Price "). Thebook.getelementsbytagname (" Price"). Item (0). InnerText =" the"; Console.Out.WriteLine ("---Revise the price of the book at this time----"); Console.Out.WriteLine (Thebook.outerxml); //---also want to add an attribute ID with a value of B01----Thebook.setattribute ("ID","B01"); Console.Out.WriteLine ("---also want to add an attribute ID with a value of B01----"); Console.Out.WriteLine (Thebook.outerxml); //---to the completion of the revision of Harry Potter. ---- //---Delete all books with a price below 10----Thebook = (XmlElement) root. selectSingleNode ("/books/book[@id = ' B02 ')"); Console.Out.WriteLine ("---Want to delete the book "Kingdoms" with the id attribute----"); Console.Out.WriteLine (Thebook.outerxml); TheBook.ParentNode.RemoveChild (Thebook); Console.Out.WriteLine ("---The deleted XML----"); Console.Out.WriteLine (xmldoc. OuterXml); //---Delete all books with a price below 10----XmlNodeList Somebooks = root. SelectNodes ("/books/book[price<10]"); Console.Out.WriteLine ("---Delete all books with a price below 10---"); Console.Out.WriteLine ("---Eligible books are"+ Somebooks.count +"this. ---"); for(inti =0; i < Somebooks.count; i++) {Somebooks.item (i). Parentnode.removechild (Somebooks.item (i)); } Console.Out.WriteLine ("---The deleted XML----"); Console.Out.WriteLine (xmldoc. OuterXml); XmlDoc. Save ("books.xml");//Save to Books.xmlConsole.In.Read (); } Catch(Exception e) {Console.Out.WriteLine (e.message); } console.readkey (); } }}
Effect:
ASP. NET-Using XML