Easy XML data processing in. NET Framework (4)

Source: Internet
Author: User
Tags processing instruction

Easy XML data processing in. NET Framework (4) Xmltextwriter ClassIt is obviously not difficult to create an XML document using the methods in this section. For many years, developers have created XML documents by caching and connecting strings and outputting cached strings to files. However, this method is only effective when you ensure that there are no small errors in the string .. Net Framework provides a better way to create XML documents by using xmlwriter. The XML writer class outputs XML data to a stream or file only in the form of forward-only. More importantly, XML writer ensures that all XML data complies with W3C XML 1.0 recommendation specifications during design. You don't even have to worry about writing closed tags Because XML writer will help you write. Xmlwriter is the abstract base class of all XML writer .. Net Framework only provides the unique writer class-xmltextwriter class. Let's take a look at the differences between XML writers and old writers. The following code saves a string-type array: stringbuilder sb = new stringbuilder (""); sb. append ("<array>"); foreach (string s in thearray) {sb. append ("<element value =/" "); sb. append (s); sb. append ("/"/> ");} sb. append ("</array>"); the Code extracts the elements in the Data cyclically, writes the label text, and adds them to a string. The Code ensures that the output content is well-formatted and pays attention to the indentation of new lines and supports namespaces. This method may not be wrong when the structure of the created document is relatively simple. However, when you want to support processing commands, namespaces, indentation, formatting, and entities, the number of codes increases exponentially, and the possibility of errors also increases. The XML writer write method function corresponds to each possible XML node type, which makes the process of creating XML documents more logical and less reliant on the tedious markup language. Figure 6 demonstrates how to use the xmltextwriter class to connect a string data. The code is concise. The code using XML writer is easier to read and has a better structure. Figure 6Serializing A String ArrayVoid createxmlfileusingwriters (string [] thearray, string filename) {// open the XML writer (using the default Character Set) xmltextwriter xmlw = new xmltextwriter (filename, null); xmlw. formatting = formatting. indented; xmlw. writestartdocument (); xmlw. writestartelement ("array"); foreach (string s in thearray) {xmlw. writestartelement ("element"); xmlw. writeattributestring ("value", S); xmlw. writeendelement ();} xmlw. writeend Document (); // close the writer xmlw. Close ();} however, XML writer is not a magician-it cannot fix input errors. XML writer does not check whether the element name and attribute name are valid, nor does it guarantee that any UNICODE character set used is suitable for the collation set of the current architecture. As mentioned above, to avoid output errors, non-xml characters must be eliminated. However, writer does not provide this method. In addition, when creating an attribute node, writer does not check whether the name of the attribute node is the same as that of an existing element node. Finally, the xmlwriter class is not a writer class with verification, and it does not guarantee that the output conforms to the schema or DTD. Currently, writer classes with verification in. NET Framework are not provided. However 《 Applied XML programming for Microsoft. Net(Microsoft Press, 2002), I wrote a writer component with verification. You can download the source code at the URL below: http://www.microsoft.com/MSPress/books/6235.asp. Figure 7 lists some State values of XML writer ). These values are derived from the writestate enumeration class. When you create a writer, its initial state is start, indicating that you are configuring the object. In fact, the writer has not started. The next status is Prolog, Which is set when you call the writestartdocument method to start working. Then, the state conversion depends on the document you wrote and the content of the document. The PROLOG status is retained until you add a non-element node, such as the annotation element, processing instruction, and document type. After the first node, that is, the root node, is written, the status changes to element. The status is converted to attribute when you call the writerstartatribute method, rather than to this status when you call the writeatributestring method to write an attribute. In that case, the status should be element. When you write a closed tag (>), the status is converted to content. After you finish writing the document, call the writeenddocument method, and the status will return start until you start writing another document or turn off writer. Figure 7States 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. Generally, the buffer zone is refreshed or cleared. XML text should be written before the writer is closed. At any time, you can call the flush method to clear the buffer, write the current content to the stream (expose the stream through the basestream attribute), and then release part of the occupied memory, writer is still in open state. You can continue the operation. Note: although some documents are written, other programs cannot process this document before the writer is closed.
You can use two methods to write attribute nodes. The first method is to use the writestartatri method to create a new attribute node and update the writer status. 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 writerr state is element, writerattributestring starts to work and it creates an attribute separately. Similarly, the writestartelement method writes the start label of a node (<), and you can set the node attributes and text content at will. "/>" Is included in all closed labels of element nodes ". You can use the writefullendelement method to write a closed tag. Do not include sensitive markup characters in the text transmitted to the writing method, such as signs smaller than (<). The string written into the stream using the writeraw method will not be parsed. We can use it to write special strings to the XML document. In the following two lines of code, the first line outputs "& lt", and the second line outputs "<": writer. writestring ("<");

Writer. writeraw ("<");

Read/write streamInterestingly, the reader and writer classes provide methods for reading and writing data streams based on base64 and binhex encoding. The functions of writebase64 and writebinhex methods are slightly different from those of other write methods. They are both stream-based. The functions of these two methods are 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 to complete the conversion task: writer. writebase64 (encoding. unicode. getbytes (BUF), 0, Buf. length * 2); the Code in Figure 8 demonstrates converting a string data to a base64 encoded XML stream. Figure 9 shows the output result. Figure 8Persisting a string array as base64Using system; using system. text; using system. io; using system. XML; Class mybase64array {public static void main (string [] ARGs) {string outputfilename = "test64.xml"; if (ARGs. length> 0) outputfilename = ARGs [0]; // file name // convert the array to XML string [] thearray = {"Rome", "New York ", "sysydney", "stockhoma", "Paris"}; createoutput (thearray, outputfilename); return;} Private Static void createoutput (string [] Thearray, string filename) {// enable XML writer xmltextwriter xmlw = new xmltextwriter (filename, null); // enable the subelement to set indentation Based on indentation and indentchar. This option only indent the element content xmlw. formatting = formatting. indented; // write the XML declaration xmlw with the version "1.0. writestartdocument (); // write comments containing the specified text <! --... -->. Xmlw. writecomment ("array to base64 XML"); // start to write the array node xmlw. writestartelement ("array"); // write the xmlw attribute with the specified prefix, local name, namespace URI and value. writeattributestring ("xmlns", "X", null, "dinoe: msdn-Mag"); // cyclically write data to the array subnode foreach (string s in thearray) {// write the specified start mark and associate it with the given namespace and prefix xmlw. writestartelement ("X", "element", null); // converts s to the byte [] array, encodes the byte [] array into base64, and writes the result text, the number of bytes to be written is twice the total length of S, and the number of bytes of a string is 2 bytes. Xmlw. writebase64 (encoding. unicode. getbytes (s), 0, S. length * 2); // close the sub-node xmlw. writeendelement ();} // closes the root node. There are only two levels of xmlw. writeenddocument (); // close writer xmlw. close (); // read the written content xmltextreader reader = new xmltextreader (filename); While (reader. read () {// obtain the node if (reader. localname = "element") {byte [] bytes = new byte [1000]; int n = reader. readbase64 (bytes, 0, 1000); string Buf = encoding. unicode. getstring (bytes); console. writeline (BUF. substring (0, n) ;}} reader. close ();}}

Figure 9String Array in Internet Explorer
Reader
Class has a dedicated interpretation of base64 and binhex encoding stream methods. The following code snippet demonstrates how to use the readbase64 method of the xmltextreader class to parse the documents created using base64 and binhex collections. Xmltextreader reader = new xmltextreader (filename); While (reader. read () {If (reader. localname = "element") {byte [] bytes = new byte [1000]; int n = reader. readbase64 (bytes, 0, 1000); string Buf = encoding. unicode. getstring (bytes); console. writeline (BUF. substring (0, n) ;}} reader. close ();

Conversion from byte type to string type is achieved through the getstring method of the encoding class. Although I only introduced the code based on the base64 encoding set, you can simply use binhex to replace the method name to read the node content based on binhex encoding (using the readbinhex method ). This technique can also be used to read any binary data expressed in bytes, especially image data.

 

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.