Easily process XML data in .NET Framework (4-2)

黄舟
Release: 2016-12-20 14:13:44
Original
1529 people have browsed it

Figure 7 States for XML Writer

State
Description

Attribute
The writer enters this state when an attribute is being written

Closed
The Close method has been called and the writer is no longer available for writing operations

Content
The writer enters this state when the content of a node is being written

Element
The writer enters this state when an element start tag is being written

Prolog
The writer is writing the prolog of a well-formed XML 1.0 document

Start
The writer is in an initial state, awaiting for a write call to be issued


??Writer stores the output text in an internal buffer. Normally, the buffer will be flushed or cleared, and the XML text should be written before the Writer is closed. At any time, you can clear the buffer by calling the Flush method, write the current content to the stream (exposed through the BaseStream property), and then release part of the occupied memory. The Writer remains open state. , can be continuously controlled. Note that although part of the document content has been written, other programs cannot process the document before the Writer is closed.

??You can use two methods to write attribute nodes. The first method is to use the WriteStartAtribute method to create a new attribute node and update the status of the Writer. Then use the WriteString method to set the property value. After writing, use the WriteEndElement method to end the node. In addition, you can also use the WriteAttributeString method to create a new attribute node. When the status of writerr is Element, WriterAttributeString starts to work and it creates a separate attribute. Similarly, the WriteStartElement method writes the node's opening tag (<), and then you can freely set the node's properties and text content. The closing tags of element nodes all have "/ >". If you want to write a closing tag, you can use the WriteFullEndElement method to write it.

??You should avoid text passed to write methods that contains sensitive markup characters, such as the less than sign (<). Strings written to the stream using the WriteRaw method will not be parsed. We can use it to write special strings to xml documents. In the following two lines of code, the first line outputs "<" and the second line outputs "<":

writer.WriteString('<');

writer.WriteRaw('<');



??Reading and writing streams

??Interestingly, the reader (browser) and writer classes provide methods for reading and writing data streams based on Base64 and BinHex encoding. The functions of the WriteBase64 and WriteBinHex methods are slightly different from those of other write methods. They are both stream-based, and these two methods function like a byte array rather than a string. The following code first converts a string into a byte array, and then writes them into a Base64 encoded stream. The GetBytes static method of the Encoding class completes the conversion task:

writer.WriteBase64(

Encoding.Unicode.GetBytes(buf),

0, buf.Length*2);

The above is in the .NET Framework Easily process XML data (4-2). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!