I originally wanted to write an Xml writer based on XmlDocument, but I found it really troublesome to maintain the status. When I read Applied Xml Programming for Microsoft. Net, I accidentally discovered the superiority of XmlTextWriter and decided to use it for development.
Differences between XmlWriter and XmlTextWriter
At first, I thought that since XmlTextWriter can be used, XmlWriter can also be used. When I finish the following statement, the compiler reports an error.
XmlWriter doc = new XmlWriter ();
The reason is simple. XmlWriter is an abstract class and cannot be directly instantiated. After reading the book, I realized that XmlTextWriter is the implementation class of the XmlWriter interface. Therefore, the initialization statement is changed
XmlWriter doc = new XmlTextWriter ();
That's right.
Common functions
1. WriteStartDocument ()
Used to write the Xml header Declaration, that is, <? Xml version = "1.0"
Encoding = "UTF-8">, if you use the function's overload function WriteStartDocument (bool
Standalone), you can set the declared standalone attribute, which indicates whether the xml document is independent from other files, that is, whether the node needs to be declared separately.
2. WriteEndDocument ()
Although this function does not write any text to the xml file, it must be called at the end of writing to end the write process. It will clear all the stack and Temporary Information Maintained in XmlTextWriter, similar to the Dispose () function.
3. WriteStartElement ()
Create a subnode of the current node. Generally, WriteStartElement (string LocalName, string Value) is used. LocalName indicates the node name, and value indicates the InnerText of the node.
Example: WriteStartElement ("address", "Tian Jing Road ");
Generated xml: <address> Tian Jing Road </address>
4. WriteEndElement ()
Once a WriteStartElement () statement is used, there must be a corresponding WriteEndElement (). When the WriteEndElement is executed, the node to which it is currently directed will be converted to the parent node, for example:
<Shop>
<Address> Tian Jing Road </address>
</Shop>
After WriteStartElement ("address", "Tian Jing Road") is executed, it points to the <address> node. After the WriteEndElement is executed, the current node points to the <shop> node.
5. WriteStartAttribute () and WriteEndAttribute ()
Same as node creation function usage
6. WriteAttributeString (string LocalName, string value)
This function is used to create attributes. Unlike WriteStartAttribute (), it does not need EndAttribute () and can be directly used.
Note that it differs from the xml generated by WriteStartAttribute. See the following example:
WriteStartElement ("address ");
WriteStartAttribute ("state", "California ");
WriteEndAttribute ();
WriteEndElement ();
Xml generated by the code above: <address d1p1: state = "" xmlns: d1p1 = "California"/>
WriteStartElement ("address ");
WriteAttributeString ("state", "California ");
WriteEndElement ();
Xml generated by the code above: <address state = "California"/>
That is to say, WriteAttributeString generates unprocessed original statements, while WriteStartAttribute () generates Xml Schema statements.
7. WriteElementString (string LocalName, string Value)
Similar to WriteAttributeString, LocalName indicates the node name and Value indicates the InnerText of the node.
8. Differences between WriteString () and WriteRaw ()
The Xml generated by WriteString ("More>") is More & gt.
The xml generated by WriteRaw ("More>") is More>
That is to say, WriteString will convert sensitive characters into escape characters, while WriteRaw () is directly written without any processing.
9. WriteCData ()
This function is used to write strings wrapped with CData. It is useful when strings contain sensitive characters.
The Xml generated by WriteCData ("More>") is <! [CDATA [More>]>
10. WriteFullEndElement ()
This function is used to write the complete end mark, such as <address> </address>,
Writer. WriteStartElement ("address", null );
Writer. WriteFullEndElement ();
Common attributes
1. The default value of the Formatting attribute is Formatting. None.
2. XmlTextReader. Indentation attribute, indicating the number of characters near the characters. It takes effect only when the Formatting attribute is Formatting. Indented.
3. XmlTextReader. IndentChar will take effect only. IndentChar is a near-length character, and '\ T' is generally used. It will take effect only when the Formatting attribute is Formatting. Indented.
4.
The QuoteChar attribute indicates closed characters. The default value is double quotation marks.