Use XmlWriter to write Xml and xmlwriter to write xml
Use XmlWriter 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 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
// 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:
// 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
xmlWriter.WriteStartElement( "dog" ); // Write CData xmlWriter.WriteCData( "<strong>dog is dog</strong>" ); xmlWriter.WriteEndElement(); |
4. How to Use XmlWriter to add comments
xmlWriter.WriteComment( "this is an example writed by http://www.baidu.com/" ); |
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(); // 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:
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.baidu.com/" ); xmlWriter.WriteEndElement(); xmlWriter.WriteEndDocument(); } // Output xml content to the console string xml = Encoding.UTF8.GetString(ms.ToArray()); Console.WriteLine(xml); } Console.Read(); } } } |
C # processing Xml:
1. read and Write Xml documents through XmlDocument 2. use XmlReader to read Xml, use XmlWriter to write Xml3. use Linq to xml to access XML4. use XmlScheme to define fixed-format xml documents. 5. xml serialization or deserialization Class 6. search for Xml nodes using XPath 7. convert Xml format using Xslt