C # Add an XML file

Source: Internet
Author: User
Tags save file

Citation: System.Xml;

XmlDocument doc =NewXmlDocument (); XmlElement Root= Doc. CreateElement ("Root");//Main contentDoc. AppendChild (Root); XmlElement Child1= Doc. CreateElement ("attr1"); XmlAttribute attr1= Doc. CreateAttribute ("attr1"); attr1. Value="arrt1content"; Child1.Attributes.Append (ATTR1); Root.appendchild (Child1);//this line and the order above cannot be reversed//arr1 in your field, such as the field with quotation marks will use \ ', it is best not to use the text Gencun content of XML//if you have 170 of your loops to pair there should be two loops one in attr1 this is used to add 150 fields one in child1 to add a few lines//Doc. InnerXml This property is your XML contentDoc. Save ("C://1.xml");//Saving this XML Web page or EXE can
View Code

XmlDocument xmldoc =NewXmlDocument ();//Declaration SectionXmlDeclaration Dec = xmldoc. Createxmldeclaration ("1.0","Utf-8",NULL); xmldoc. AppendChild (DEC);//Join a root nodeXmlElement Onenode = xmldoc. CreateElement ("CATALOG");//Create a nodeXmlElement Twonode = xmldoc. CreateElement ("CD"); XmlElement Twonodeone= xmldoc. CreateElement ("TITLE"); Twonodeone.innertext="Empire Burlesque"; Twonode.appendchild (twonodeone);//Add to CD node belowOnenode.appendchild (TWONODE1);//add to below catalog nodexmldoc. AppendChild (Onenode); xmldoc. Save (Server.MapPath ("")+"/1.xml");//Save XML
View Code

XPath can be used to traverse elements and attributes in an XML document. But it can't be used to create XML, and creating XML typically uses XmlWriter, XmlDocument, or Linq2xml.
/// <summary>method Description ..... Creating an XML document/// </summary>        /// <param name= "XMLFile" >the name of the document to save without adding a suffix</param>       Private voidFabao_senior (stringxmlfile) {XmlDocument Xdoc=NewXmlDocument ();//creating an XML Document ObjectXmlNode xnode = Xdoc. Createxmldeclaration ("1.0","GB2312","Yes"); Xdoc.             AppendChild (XNode); /*when creating, it is best to add all the node elements in/* Create the root node*/XmlElement Rootele= Xdoc. CreateElement ("","sysstring",""); Xdoc.             AppendChild (Rootele); // //find sysstring root nodeXmlNode roots = Xdoc. selectSingleNode ("sysstring"); XmlElement Total= Xdoc. CreateElement ("toTable"); /*Creating a FileName node element*/XmlElement gamename= Xdoc. CreateElement ("Name"); /*... See Total.setattribute () above ...*/Gamename.innertext="Null";             Total.appendchild (Gamename); XmlElement Name= Xdoc. CreateElement (" Age"); Name.innertext="Null";             Total.appendchild (Name); /*☆☆☆☆☆☆☆☆☆☆☆☆☆☆*/              for(inti = $; I <= -; i++)            {                CharQ = (Char) I; XmlElement Elename= Xdoc. CreateElement ("Char_"+q); Elename.innertext="True/false";            Total.appendchild (Elename); }              for(inti = the; I <=122; i++)            {                CharQ = (Char) I; XmlElement Elename= Xdoc. CreateElement ("Char_"+q); Elename.innertext="True/false";            Total.appendchild (Elename); }             /*include total at the end of the root node*/roots.             AppendChild (total); //Save the created XML documentXdoc. Save (XMLFile +". XML");//Save File}
View Code
The XmlTextWriter object contains a number of methods that can be used to add elements and attributes to an XML file when creating an XML file, and it is important to:

WriteStartDocument ()-Creating an XML file requires this method first, which is the first line of code to create an XML file that specifies that the file is an XML file and sets its encoding type;

WriteStartElement (String)-the function of this method is to create a new element in the XML file, you can set the name of the element through the string parameter (you can also use the optional keyword to specify an optional parameter, of course);

WriteElementString (name, Text_value)-You can use this method if you need to create an element that has nothing but a character, such as a nested element;

WriteEndElement ()-Corresponds to the WriteStartElement (string) method, as the end of an element;

WriteEndDocument ()-xml after the file is created, use this method to finish;

Close ()-Closes all text streams and outputs the created XML file to the specified location.

To create an XML file using the XmlTextWriter object, you specify the type of the file in the class constructor, and the encoding type must be System.Text.Encoding, such as: System.Text.Encoding.ASCII, System.Text.Encoding.Unicode and System.Text.Encoding.UTF8, in the XmlTextWriter class constructor, specify why the type is output in the output XML file as that stream file.
C # creates an XML file using the XmlTextWriter object to create a simple XML file:
 XmlTextWriter writer=new  XmlTextWriter (Server.MapPath ( " phone4.xml  " ), null  ); Writer. formatting  = formatting.indented; //  indent format  writer. Indentation =4   First we need to be aware of the import System.Xml and System.Text namespace, then we create an instance of the XmlTextWriter object in the Page_Load event, and specify that the XML file created is saved as a userinfo.xml file and that its encoding type is UTF8 (a translation of  16 -bit Unicode encoding into 8 - bits), and then uses the WriteStartElement (ElementName) method to create an element that has nested other elements and ends with WriteEndElement (). In addition, we use the WriteElementString (ElementName, TextValue) method to create the lowest level element that does not nest other elements. 
View Code
System.Xml.XmlDocument Xml =NewSystem.Xml.XmlDocument (); System.Xml.XmlDeclaration Dec= XML. Createxmldeclaration ("1.0","UTF-8",NULL); Xml.        AppendChild (DEC); System.Xml.XmlElement Ele= XML. CreateElement ("A"); Xml.        AppendChild (ele); System.Xml.XmlElement Ele2= XML. CreateElement ("B"); System.Xml.XmlAttribute xa= XML. CreateAttribute ("C"); Xa. Value="D"; Ele2.        Attributes.append (XA); Ele2. INNERXML="E"; Ele.         AppendChild (Ele2); Xml. Save (Server.MapPath ("~/1.xml"));
View Code

Study site: http://www.bccn.net/Article/web/xml/

C # Add an XML file

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.