Home  >  Article  >  Backend Development  >  Easily process XML data in .NET Framework (1-1)

Easily process XML data in .NET Framework (1-1)

黄舟
黄舟Original
2016-12-20 14:01:311850browse

??In the .NET Framework, the XmlTextReader and XmlTextWriter classes provide read and write control of xml data. In this article, the author describes the system structure of XML browsers (Readers) and how they are combined with XMLDOM and SAX specifiers. The author also demonstrates how to use the browser to analyze and validate XML documents, how to create well-formatted XML documents, and how to use functions to read/write large XML documents based on Base64 and BinHex encoding. Finally, the author talks about how to implement a stream-based read/write analyzer, which encapsulates readers and writers in a separate class.

??About three years ago, I participated in a software research conference, and the theme was "Without XML, there will be no future of programming." XML is indeed developing step by step, and it has been embedded in the . NET Framework. In this article, I will explain the role of the API for processing XML documents in the . NET Framework and its internal characteristics, and then I will demonstrate some commonly used functions.

??From MSXML to .net XML

??Before the emergence of . NET Framework, you were used to using the MSXML service - a COM-based class library - to write XML drivers for Windows. Unlike the classes in the . NET Framework, part of the code of the MSXML class library is deeper than the API, and it is completely embedded in the bottom layer of the operating system. MSXML can indeed communicate with your application, but it cannot truly integrate with the external environment.

??The MSXML class library can be imported in win32 and can also be used in the CLR, but it can only be used as an external server component. However, applications based on the .NET Framework can directly use XML classes to integrate applications with other namespaces of the .NET Framework, and the written code is easy to browse.

??As an independent component, MSXML parser provides some advanced features such as asynchronous analysis. This feature is not provided in the XML class in the .NET Framework and other classes in the .NET Framework. However, the XML class in the NET Framework can be easily integrated with other classes to obtain the same functions. On this basis, you can add More efficacy.

??The XML class in .NET Framework provides basic functions for analyzing, querying, and converting XML data. In the .NET Framework, you can find classes that support Xpath queries and XSLT transformations, as well as classes for reading/writing XML documents. In addition, the .NET Framework also contains other classes for processing XML, such as object serialization (XmlSerializer and the SoapFormatter class), application configuration (AppSettingsReader class), and data storage (DataSet class). In this article, I only discuss classes that implement basic XML I/O operations.

??XML analysis mode

??Since XML is a markup language, there should be a tool to analyze and understand the information stored in the document according to a certain syntax. This tool is an XML parser—a component that reads marked text and returns platform-specific objects.

??All XML parsers, no matter which operating platform they belong to, are divided into the following two categories: tree-based or event-based processors. These two categories are usually implemented using XMLDOM (the Microsoft XML Document Object Model) and SAX (Simple API for XML). The XMLDOM parser is a generic tree-based API - it exposes the XML document as a tree of in-memory structures. The SAX parser is an event-based API - it processes each 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 these two types of processing are not mutually exclusive.

??In general, SAX parser and XMLDOM parser are exactly the opposite, and their analysis modes are very different. XMLDOM is well defined within its functionalition collection, you cannot extend it. When it is processing a large document, it takes up a lot of memory space to process the huge collection of functionalitions.

??The SAX analyzer uses client applications to process analysis events through existing instances of platform-specific objects. The SAX parser controls all processing, "pushing" the data to the handler, which in turn accepts or rejects the data for processing. The advantage of this mode is that it requires very little memory space.

??.NET Framework fully supports XMLDOM mode, but it does not support SAX mode. why? Because the .NET Framework supports two different analysis modes: XMLDOM parser and XML browser. It obviously doesn't support a SAX parser, but that doesn't mean it doesn't offer SAX parser-like functionality. All the functions of SAX through XML browser can be easily realized and used more effectively. Unlike SAX parsers, .NET Framework browsers all run under client applications. In this way, the application itself can "pull out" only the data that is really needed, and then jump out of the XML data stream. The SAX analysis mode handles all useful and useless information for the application.

??The browser works based on the .NET Framework streaming mode, and its working method is similar to a database cursor. Interestingly, a class that implements a similar cursor parsing pattern provides underlying support for the XMLDOM parser in the .NET Framework. The two abstract classes XmlReader and XmlWriter are the basic classes of all XML classes in the .NET Framework, including the XMLDOM class, ADO.NET driver class and configuration class. So you have two alternative ways to process XML data in the .NET Framework. Use the XmlReader and XmlWriter classes to process XML data directly, or use the XMLDOM mode. For more information about reading documents in the .NET Framework, see the Cutting Edge column article in the August 2002 issue of MSDN.


The above is the content of easily processing XML data (1-1) in .NET Framework. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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