The. NET framework handles XML data easily (i)

Source: Internet
Author: User
Tags xml parser xml reader

In. NETFramework, the XmlTextReader and XmlTextWriter classes provide read and write operations on XML data. In this article, the authors describe the architecture of the XML Reader (reader) and how they are combined with the XMLDOM and sax interpreters. The authors also demonstrated how to use the reader to analyze and validate XML documents, how to create well-formed XML documents, and how to read/write large XML documents based on BASE64 and BinHex encoded by functions. Finally, the author tells how to implement a stream based read/write analyzer, which encapsulates the reader in a separate class.

About three years ago, I attended a software workshop with the theme "No XML, no programming future." XML is indeed evolving step-by-step, it has been embedded. In the NETFramework. In this article, I'll explain. NETFramework the role of the API for processing XML documents and its internal features, and then I'll demonstrate some of the common features.

XML from MSXML to. Net

Before. NETFramework appears, you are accustomed to using the MSXML service----a COM-based class library---write Windows XML drivers. Not like. Class in NETFramework, some of the code in the MSXML class library is deeper than the API, and it is completely embedded in the bottom of the operating system. MSXML does have the ability to communicate with your application, but it does not really combine with the external environment.

The MSXML class library can be imported into the Win32, and can be used in the CLR, but it can only be used as an external server component. But based on. NETFramework applications can be used directly with XML classes and other namespaces of. NETFramework, and the written code is easy to read.

As a stand-alone component, the MSXML parser provides advanced features such as asynchronous parsing. This feature is in the. The XML class in the NETFramework and the other classes of. NETFramework are not provided, but the XML class in NETFramework can easily get the same functionality with other classes, and you can add more functionality on that basis.

. The XML classes in NETFramework provide basic functionality for parsing, querying, and transforming XML data. In. NETFramework, you can find classes that support XPath queries and XSLT transformations, and read/write XML documents. Other than that. NETFramework also contains other classes that process XML, such as serialization of objects (XmlSerializer and Thesoapformatter classes), application configuration (Appsettingsreader classes), data storage (DataSet classes). In this article, I only discuss classes that implement basic XMLI/O operations.

XML parsing mode

Since XML is a markup language, there should be a tool that analyzes and understands the information stored in the document in a certain syntax. This tool is the XML parser---a component that reads the markup text and returns the object for the specified platform.

All XML parsers, regardless of which operating platform they belong to, are divided into two categories: tree-based or event-based. These two classes are usually implemented using XMLDOM (Themicrosoftxmldocumentobjectmodel) and sax (simpleapiforxml). The XMLDOM parser is a generic, tree-based API---it renders an XML document as a memory structure tree. The SAX parser is an event-based API----it processes every element in the XML data stream (it puts the XML data into the stream and processes it). Typically, the DOM can be loaded and executed by a sax stream, so the two classes of processing are not mutually exclusive.

In general, the SAX parser is just the opposite of the XMLDOM parser, and their analysis patterns are very different. XMLDOM is well defined in its functionalition set, you can't expand it. When it is working on a large document, it takes up a lot of memory to handle the huge collection of functionalition.

The SAX parser uses client applications to handle profiling events through an instance of an existing, specified platform object. The SAX analyzer controls the entire process and "launches" the data into the handler, which in turn accepts or rejects processing data. The advantage of this pattern is that you need very little memory space.

. NETFramework fully supports the XMLDOM mode, but it does not support sax mode. Why, then? Because. NETFramework supports two different profiling modes: The XMLDOM parser and the XML reader. It obviously does not support the SAX parser, but that does not mean that it does not provide functionality similar to the SAX parser. All of the functionality of Sax through the XML reader is easy to implement and more efficient to use. Unlike the SAX parser,. The NETFramework reader operates entirely under the client application. In this way, the application itself can "roll out" the data that is really needed, and then jump out of the XML data stream. The SAX parsing model handles all the information that is useful and useless to the application.

The reader is based on. NETFramework flow mode works in a way that is similar to a database cursor. Interestingly, a class that implements a cursor profiling pattern provides a pair. The underlying support for the XMLDOM parser in NETFramework. XmlReader, XmlWriter Two abstract classes are the base class for all. NETFramework XML classes, including Xmldom class, ADO. NET driver class and configuration class. So in. In NETFramework you have two options for working with XML data. Process XML data directly with XmlReader and XmlWriter classes, or in XMLDOM mode. More about in. NETFramework in the introduction of the document can be seen in the MSDN2002 August issue of the Cuttingedge column article.

XmlReader class

The XML reader supports a programming interface that interfaces with XML documents and "launches" the data you want. If you go deeper into the reader, you'll find that the reader works like the way our desktop application pulls data out of the database. The database service returns a cursor object that contains all the query result sets and returns a reference to the start address of the destination dataset. The client of the XML reader receives a reference to the reader instance. The instance extracts the underlying data stream and renders the extracted data as an XML tree. The reader class provides a read-only, forward-only cursor, and you can scroll through each of the data in the result set by scrolling the cursor in the method provided by the reader class.

From the reader, the XML document is not a label text file, but rather a serialized node collection. It is. A special cursor pattern in netframework; in. NETFramework, you cannot find any of the other similar API functions.

There are several different places for readers and xmldom analyzers. XML reader is only entered, it does not have the concept of parent, child, ancestor, sibling node, and is read-only. In. NETFramework, reading and writing XML documents is divided into two completely different functions, which are done by XmlReader and XmlWriter classes respectively. To edit an XML document, you can do both by using the XMLDOM parser, or by designing a class yourself. Let's start by analyzing the reader's program features.

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.