Easy processing of XML data in the. NET Framework (4-2)

Source: Internet
Author: User
Figure 7 states for XML Writer

State
Description

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

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

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

Element
The writer enters this state when a 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 under an initial state and awaiting for a write call to be issued


?? Writer puts the output text inside a buffer. Normally, the buffer is flushed or erased, and the XML text should be written out before the writer is closed. At any time, you can flush the buffer by calling the Flush method, write the current content to the stream (through the BaseStream property * * *), and then release the portion of memory occupied, writer remains open (open state), can continue to control. Note that while writing part of the document content, other programs cannot process the document until the writer has closed it.

?? You can write attribute nodes in two ways. The first method is to use the Writestartatribute method to create a new attribute node to update the writer's state. The property value is then set using the WriteString method. After writing, use the WriteEndElement method to finish the node. In addition, you can also use the WriteAttributeString method to create a new attribute node, when the state of Writerr is element, writerattributestring start work, it creates a single attribute. Similarly, the WriteStartElement method writes the node's start tag (<), and then you can set the node's properties and text content casually. The closed tags of the element nodes are all with "/>". If you want to write closed tags can be written in Writefullendelement method.

?? The text that is passed to the write method should be avoided including sensitive tag characters, such as the less than sign (<). The string written into the stream using the WriteRaw method is not parsed, and we can use it to write a special string to the XML document. The following two lines of code, the first line of output is "<", the second line output "<":

Writer. WriteString (' < ');

Writer. WriteRaw (' < ');



?? Read/write Stream

?? Interestingly, reader (browser) and writer classes provide a way to read and write data streams based on Base64 and BinHex encoding. There are subtle differences between the efficacy of the WriteBase64 and Writebinhex methods and the efficacy of other writing methods. They are all flow-based, and the two methods function like a byte array instead of 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 task of conversion:

Writer. WriteBase64 (

Encoding.Unicode.GetBytes (BUF),

0, buf. LENGTH*2);

The above is the easy processing of XML data (4-2) in the. NET framework, and more about topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.