XmlTextWriter write a file, the default is to overwrite the previous file, if the file name does not exist, it will create this file, first create the XML file format
1.
XmlTextWriter myXmlTextWriter = new XmlTextWriter (".. \.. \xxx.xml ", NULL);
2. Use the Formatting property to specify what format you want to set the XML to. Elements like this can be indented by using the indentation and IndentChar properties.
myXmlTextWriter.Formatting = formatting.indented;
The elements can then be created using the WriteStartElement and WriteElementString methods, the difference being that if there are elements of a child node, then it is created with writestartelement and then to create the element
Once created, call the appropriate writeendelement to tell the compiler, create a single element with writeelementstring, and create the attribute with WriteAttributeString. as follows:
1:xmltextwriter myXmlTextWriter = new XmlTextWriter (@ "... \..
\book1.xml ", NULL); 2://Use the Formatting property to specify what format you want to set the XML to.
In this way, child elements can be indented by using the indentation and IndentChar properties.
3:myxmltextwriter.formatting = formatting.indented;
4:5: Myxmltextwriter.writestartdocument (FALSE);
6:myxmltextwriter.writestartelement ("bookstore");
7:8: myXmlTextWriter.WriteComment ("Information for recording books");
9:myxmltextwriter.writestartelement ("book");
10:11:myxmltextwriter.writeattributestring ("Type", "elective");
12:myxmltextwriter.writeattributestring ("ISBN", "111111111");
13:14:myxmltextwriter.writeelementstring ("Author", "Zhang San");
15:myxmltextwriter.writeelementstring ("title", "career Plan");
16:myxmltextwriter.writeelementstring ("Price", "16.00");
17:18:myxmltextwriter.writeendelement ();
19:myxmltextwriter.writeendelement (); 20:
21:myxmltextwriter.flush (); 22:myxmltextwriter.close ();
Raise:
WriteStartDocument () method
WriteStartDocument becomes the writer validation, and you are writing a well-formed XML document. For example, he checks that the XML declaration is the first node, and that only one root-level element exists, and so on.
If this method is not called, the writer assumes that an XML fragment is being written and does not apply any root-level rules.