C # XML Class learning finishing (backlog)

Source: Internet
Author: User

First, read the contents of the XML file:

         #region"Get XML file Contents"//load the XML file inXDocument Xdoc = Xdocument.load ("f:\\6.2 \\DelegateExample\\XmlConsole\\XMLFile.xml"); //get XML root element operationXElement root =Xdoc.            Root; XElement Elem= root. Element (" Book"); //get the value of the name tagXElement name = Elem. Element ("name1"); Console.WriteLine (name.            Value); Console.WriteLine ("------------------------"); //gets all child elements under the root elementienumerable<xelement> element =Root.            Elements (); foreach(XElement IteminchElement) {                foreach(XElement item1inchitem. Elements ()) {Console.WriteLine (item1.                Value); } Console.WriteLine ("------------------------"); Console.WriteLine (item. Attribute ("ID").                Value);            Console.readkey (); }            #endregion                

Second, write the XML content:

         //get root node elementXDocument XDocument =NewXDocument (); XElement Roo=NewXElement ("b"); XElement Root2=NewXElement (" Book",NewXElement ("Name3","3333",NewXAttribute ("ID","3"))); XElement name1=NewXElement ("name1"); XElement ROOT3=NewXElement ("Student"); Root2. Setelementvalue ("name1","1111"); Root2. Setelementvalue ("name2","2222"); Root3. Setattributevalue ("Student1","33333"); Root3. Setelementvalue ("Student2","44444"); Roo.            ADD (NAME1); Roo.            ADD (ROOT2); Roo.            ADD (ROOT3); Roo. Save ("D://xmlfile.xml"); Console.readkey ();

Thirdly, XMLNode is an abstract class, which provides some methods for manipulating nodes.

        //take root node.          varroot = xmldoc.documentelement;//take the root node.//to take a specified single node .XmlNode Oldchild = Xmldoc.selectsinglenode ("Bookstore/newbook"); //takes a collection of specified nodesXmlNodeList nodes = Xmldoc.selectnodes ("Bookstore/newbook"); //fetch all XML nodes .XmlNodeList nodelist = Xmldoc.getelementsbytagname ("*");

Add, delete (remove), modify node and attribute Setattributevalue () + Save (),

removechild  --Removing nodes       RemoveAttribute--Removing attributes

V. LINQ to XML (XML additions and deletions)

C # XML Class learning finishing (backlog)

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.