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
Copy codeThe Code is as follows: // The WriteStartDocument method can accept a bool parameter (standalone, whether it is an independent document) or keep the default value when standalone is not specified.
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
Copy codeThe Code is as follows: // 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:Copy codeThe Code is as follows: // 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
Copy codeThe Code is as follows: xmlWriter. WriteStartElement ("dog ");
// Write CData
XmlWriter. WriteCData ("<strong> dog is dog </strong> ");
XmlWriter. WriteEndElement ();
4. How to Use XmlWriter to add comments
Copy codeThe Code is as follows: xmlWriter. WriteComment ("this is an example writed by http://www.jb51.net ");
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:Copy codeThe Code is as follows: 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:Copy codeThe Code is as follows: 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 http://www.jb51.net ");
XmlWriter. WriteEndElement ();
XmlWriter. WriteEndDocument ();
}
// Output xml content to the console
String xml = Encoding. UTF8.GetString (ms. ToArray ());
Console. WriteLine (xml );
}
Console. Read ();
}
}
}