Detailed introduction to the sample code for writing Xml using XmlWriter

Source: Internet
Author: User
Assume that the XmlWriter instance variable is created. the following example uses this instance variable to write Xml. assume that the xmlWriter instance variable is created. the following example uses this instance variable to write Xml.

1. how to use XmlWriter to write Xml document declarations

// The WriteStartDocument method can accept a bool parameter (standalone, whether it is an independent document) or the standalone parameter is not specified. keep the default value xmlWriter. WriteStartDocument (false | true );

Note: after using the WriteStartDocument method, it is best to call the xmlWrite. WriteEndDocument () method to close all labels that may not be closed.
2. how to use XmlWriter to write xml nodes and attributes

// Write node xmlWriter. writeStartElement ("cat"); // add the xmlWriter attribute to the node. writeAttributeString ("color", "white"); // add text xmlWriter to the node. writeString ("I'm a cat"); xmlWriter. writeEndElement ();

Or write the node value at the same time by using the WriteElementString (string, string) method to write the xml node, as shown below:

// You can use WriteElementString to add a node and xmlWriter. WriteElementString ("pig", "pig is great ");

3. how to write CData

XmlWriter. WriteStartElement ("dog"); // write CDataxmlWriter. WriteCData ("Dog is dog"); XmlWriter. WriteEndElement ();

4. how to use XmlWriter to add comments

XmlWriter. WriteComment ("this is an example writed by Yukai Technical Blog #");

5. how to set the output format of XmlWriter to solve the problem of output UTF-16
To set the xml output format, use the XmlWriterSettings class. the following code:

XmlWriterSettings settings = new XmlWriterSettings (); // required indent settings. indent = true; // NOTE If encoding is not set, the UTF-16 will be output by default. // note that Encoding cannot be used directly here. UTF8 if Encoding. UTF8 adds four bytes of non-xml content at the beginning of the output text settings. encoding = new UTF8Encoding (false); // set the line feed settings. newLineChars = Environment. newLine;


The complete code example is as follows:


/* Yukai Technical Blog # */using System; using System. collections. generic; using System. text; using System. IO; using System. xml; namespace UseXmlWriter {class Program {static void Main (string [] args) {using (MemoryStream MS = new MemoryStream () {XmlWriterSettings settings = new XmlWriterSettings (); // indent settings. indent = true; // NOTE If encoding is not set, the UTF-16 will be output by default. // note that Encoding cannot be used directly here. UTF8 if Encoding. UTF8 adds four bytes of non-xml content at the beginning of the output text settings. encoding = new UTF8Encoding (false); // set the line feed settings. newLineChars = Environment. newLine; using (XmlWriter xmlWriter = XmlWriter. create (MS, settings) {// write the xml file to start
 XmlWriter. writeStartDocument (false); // write the root node xmlWriter. writeStartElement ("root"); // write node xmlWriter. writeStartElement ("cat"); // add the xmlWriter attribute to the node. writeAttributeString ("color", "white"); // add text xmlWriter to the node. writeString ("I'm a cat"); xmlWriter. writeEndElement (); // you can use WriteElementString to add a node and xmlWriter. writeElementString ("pig", "pig is great"); xmlWriter. writeStartElement ("dog"); // write CData xmlWriter. writeCData ("Dog is dog"); XmlWriter. writeEndElement (); xmlWriter. writeComment ("this is an example writed by Yukai Technical Blog #"); xmlWriter. writeEndElement (); xmlWriter. writeEndDocument ();} // output the xml content to string xml = Encoding in the console. UTF8.GetString (ms. toArray (); Console. writeLine (xml);} Console. read ();}}}

The above section details the sample code for writing Xml using XmlWriter. For more information, see other related articles in the first PHP community!

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.