Use XmlWriter to write Xml

Source: Internet
Author: User

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

View sourceprint? // The WriteStartDocument method can accept a bool parameter (standalone, whether it is an independent document) or the standalone parameter is not specified to retain 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

View sourceprint? // Write Node

XmlWriter. WriteStartElement ("cat ");

// Add attributes to a node

XmlWriter. WriteAttributeString ("color", "white ");

// Add text to the node

XmlWriter. 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:

View sourceprint? // You can use WriteElementString to add a node and add the node content at the same time.

XmlWriter. WriteElementString ("pig", "pig is great ");

3. How to Write CData

View sourceprint? XmlWriter. WriteStartElement ("dog ");

// Write CData

XmlWriter. WriteCData ("<strong> dog is dog </strong> ");

XmlWriter. WriteEndElement ();

4. How to Use XmlWriter to add comments

View sourceprint? XmlWriter. WriteComment ("this is an example writed by yukai technology blog http://www.cnblogs.com/yukaizhao ");

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:

View sourceprint? XmlWriterSettings settings = new XmlWriterSettings ();

// Requires indentation

Settings. Indent = true;

// Note if encoding is not set, the UTF-16 will be output by default

// Note that Encoding. UTF8 cannot be used directly here. If Encoding. UTF8 is used, four bytes of non-xml content will be added at the beginning of the output text.

Settings. Encoding = new UTF8Encoding (false );

// Set the line break

Settings. NewLineChars = Environment. NewLine;


The complete code example is as follows:


View sourceprint? /* Yukai technology blog http://www.cnblogs.com/yukaizhao */

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 ();

// Requires indentation

Settings. Indent = true;

// Note if encoding is not set, the UTF-16 will be output by default

// Note that Encoding. UTF8 cannot be used directly here. If Encoding. UTF8 is used, four bytes of non-xml content will be added at the beginning of the output text.

Settings. Encoding = new UTF8Encoding (false );

// Set the line break

Settings. NewLineChars = Environment. NewLine;

Using (XmlWriter xmlWriter = XmlWriter. Create (MS, settings ))

{

// Start writing an xml file <? Xml version = "1.0" encoding = "UTF-8"?>

XmlWriter. WriteStartDocument (false );

// Write the root node

XmlWriter. WriteStartElement ("root ");

// Write Node

XmlWriter. WriteStartElement ("cat ");

// Add attributes to a node

XmlWriter. WriteAttributeString ("color", "white ");

// Add text to the node

XmlWriter. WriteString ("I'm a cat ");

XmlWriter. WriteEndElement ();

// You can use WriteElementString to add a node and add the node content at the same time.

XmlWriter. WriteElementString ("pig", "pig is great ");

XmlWriter. WriteStartElement ("dog ");

// Write CData

XmlWriter. WriteCData ("<strong> dog is dog </strong> ");

XmlWriter. WriteEndElement ();

XmlWriter. WriteComment ("this is an example writed by yukai technology blog http://www.cnblogs.com/yukaizhao ");

XmlWriter. WriteEndElement ();

XmlWriter. WriteEndDocument ();

}

// Output xml content to the console

String xml = Encoding. UTF8.GetString (ms. ToArray ());

Console. WriteLine (xml );

}

Console. Read ();

}

}

}

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.