This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

After reading and writing XML using XmlDocument, XmlDocument gets XML and using XmlWriter to write XML and get text, let’s take a look at how XmlWriter reads and writes XML.

XmlWriter class

Represents a writer that provides a fast, uncaches, and forward-only way to generate a stream or file containing XML data. Using XmlWriter, you can:

  • Check that characters are valid XML characters and that element and attribute names are valid XML names.
  • Check that the XML document is properly formatted.
  • Encode binary bytes as Base64 or BinHex and write out the resulting text.
  • Use common language runtime types instead of strings to pass values to avoid having to perform value conversions manually.
  • Writes multiple documents to a single output stream.
  • Write out a valid name, qualified name, and name tag.

XmlWriter sample

Let’s use the XML structure document from the previous article as an example to demonstrate how to write XML documents.

<? The XML version = "1.0"? > <books> <book> <author> <price format="dollar">31.95</price> <pubdate>05/01/2001</pubdate> </book> <pubinfo> <publisher>MSPress</publisher> <state>WA</state> </pubinfo> </books>Copy the code

We first create the XmlWriter object, and since XmlWriter is a data flow, we also close the XmlWriter data flow. To create an object, we need to use XmlWriterSettings, so we can set the encoding format of the XML, the default is UTF-16, we set UTF-8. Then set the newline symbol, use the MemoryStream to store the written data, and Create xmlWriter.create (ms, Settings). The WriteStartElement method starts to write a node. WriteEndElement Write node ends. WriteElementString writes the entire element node, including string values. WriteAttributeString writes the entire attribute node, including string values.

XmlWriter xmlWriter = null; XmlWriterSettings settings = new XmlWriterSettings(); Settings. Indent = true; // Set encoding to utF-8. By default, utF-16 settings. encoding = new UTF8Encoding(false); // Set the NewLine character settings.NewLineChars = environment. NewLine; MemoryStream ms = new MemoryStream(); try { xmlWriter = XmlWriter.Create(ms, settings); xmlWriter.WriteStartDocument(); / / books start xmlWriter. WriteStartElement (" books "); / / book begin xmlWriter. WriteStartElement (" book "); xmlWriter.WriteElementString("author","Carson"); XmlWriter. WriteElementString (" price ", "19.95"); writer.WriteAttributeString("format", "dollar"); xmlWriter.WriteElementString("pubdate", "05/01/2001"); / / book end xmlWriter WriteEndElement (); / / pubinfo begin xmlWriter. WriteStartElement (" pubinfo "); xmlWriter.WriteElementString("publisher","MSPress"); xmlWriter.WriteElementString("state","WA"); / / end pubinfo xmlWriter WriteEndElement (); / / books end xmlWriter. WriteEndElement (); } finally { if (xmlWriter ! = null) xmlWriter.Close(); ms.Close(); } string xmlStr=Encoding.UTF8.GetString(ms.ToArray());Copy the code

With the XmlWriter demonstration, you can write XML documents and get XML text directly to use. Other methods for adding comments, namespaces, and so on are similar.