C # Two general ways of reading and writing XML

Source: Internet
Author: User

In the application programming interface for XML documents, there are generally two kinds of models: the DOM (Document object method) and the flow model developed by the consortium.

Two variants of the flow model: the "push" model (Simple API for XML) and the "pull" model (. NET in the flow model.

Parsing methods FOR XML:

Pull model

td>

 

Push model

dom

 

Push model is often called sax , Sax is an event-driven model. It uses a "push" model to raise an event every node it discovers.

. NET in the XML parsing is based on the "pull" model implementation scheme. The pull model pulls the part of interest from the reader when it traverses the document and does not need to raise events. The pull model in. NET implements the

Through the XML Reader (XmlTextReader class). NET implements the DOM model using the XML DOM parser (XMLDocument).

Cons

You must write handlers for these events, which is cumbersome and complex. The document cannot be randomly accessed and modified. The

XmlTextReader class is read-only, forward, and can no longer perform a backward navigation operation in the document.

Requires a one-time load of the entire document into memory, which can cause resource problems for large documents.

Benefits

Do not need to fully load the XML document into memory, it is an efficient API for parsing large XML documents. The

Allows us to access the document in a programmatic way, greatly increasing the flexibility to selectively process nodes, saving only the current node in memory. The XmlTextReader class can verify that the document is well-formed, and if it is not well-formed XML, the class throws a XmlException exception during the read process. The

Allows you to edit and update XML documents, Access data in the document randomly, and use XPath queries.

The. NET Framework supports the XML DOM parser (XmlDocument Class) and the XML Reader (XmlTextReader class) and does not support the SAX parser. However, it is easy to implement all the functions of sax and use it more effectively through the XML reader.

The following classes of XML are organized into the System.Xml namespace:

    1. The XmlTextReader---provides fast, one-way, unbuffered access to XML data. (one-way means that you can only read the XML file from the go and not reverse-read)
    2. XmlValidatingReader---is used with the XmlTextReader class to provide the ability to validate DTDs, XDR, and XSD schemas.
    3. XMLDocument---Follow the standard of the first and two levels of the Document Object Model specification and implement random, cached access to XML data. The first level contains the most basic parts of the DOM, while the second level adds a number of improvements, including increased support for namespaces and cascading charts (CSS).
    4. The XmlTextWriter---generates an XML file that follows the 1.0 specification.

C # Two general ways of reading and writing XML

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.