C # manipulating XML

Source: Internet
Author: User

1. Writing XML to the file

XmlDocument xmldoc =NewXmlDocument ();//building a DOM object in memoryXmlDeclaration XMLD = xmldoc.createxmldeclaration ("1.0","Utf-8","Yes");//Specifying document DeclarationsXmldoc.appendchild (XMLD);//Add a document description to the documentXmlElement rootelement= Xmldoc.createelement ("School");//Create a root element, schoolXmldoc.appendchild (rootelement);//Add the root element inXmlElement xmlclasselement= Xmldoc.createelement ("class");//add child elements to the root element (school)XmlAttribute attr = Xmldoc.createattribute ("ID");//Add a property id for class,attr. Value ="C01";//The value of ID is c01XmlClassElement.Attributes.Append (attr);//add an attribute to the class tag. Rootelement.appendchild (xmlclasselement);//add the class element to the school tagXmlElement xmlstudentelement= Xmldoc.createelement ("Student");//Create a student elementattr = Xmldoc.createattribute ("Sid");//to create a property called Sidattr. Value ="s011";//the value of the S011 property isXmlStudentElement.Attributes.Append (attr);//add an attribute to the elementXmlclasselement.appendchild (xmlstudentelement);//Add the student node to the class nodeXmlElement xmlnameelement= Xmldoc.createelement ("name"); Xmlnameelement.innertext="James"; XmlElement xmlageelement= Xmldoc.createelement (" Age"); Xmlageelement.innertext=" -";            Xmlstudentelement.appendchild (xmlnameelement);            Xmlstudentelement.appendchild (xmlageelement); Xmldoc.save ("School.xml");//writes the DOM object to an XML file
compatible with. NET 2.0 versions

XDocument XDoc =NewXDocument ();//Create a DOM objectXdeclaration xdeclaration =NewXdeclaration ("1.0","Utf-8",NULL);//declaring document TypesXdoc.declaration = xdeclaration;//Add Document TypeXElement rootelement =NewXElement ("School");//Creating the root nodeXdoc.add (rootelement);//Add the school node to the documentXElement Xclass =NewXElement ("class");//create a class elementXclass.setattributevalue ("ID","C01");//add attributes and values to the class tag. Rootelement.add (Xclass); XElement Student=NewXElement ("Student"); Student. Setattributevalue ("ID","s001");//setting properties for student nodesStudent. Setelementvalue ("Name","Curry");//in the Student node, add the leaf node name with a value of curryStudent. Setelementvalue (" Age", -);//in the Student node, add the leaf node age, the value isStudent. Setelementvalue ("Sex","male");//in the Student node, add the leaf node sex with a value of maleXclass.add (student);//Add the student node to the class node. Xdoc.save ("Xuexiao.xml");//If the file does not exist, then one is created and the content is written. If the file exists, it will overwrite the content. 
3.0 later versions

C # manipulating XML

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.