XML reference: XmlReader details, example (1)-Details

Source: Internet
Author: User
Tags cdata

. NET Framework class library

XmlReader class

 

Provides a fast, non-cache, and only-in-access reader for XML data streams, that is, only-in-read-only access to XML data streams. The XmlReader class complies with W3C Extensible Markup Language (XML) 1.0 and "namespace in XML" recommendations.

XmlReader triggers XmlException when an XML analysis error occurs. When an exception is thrown, the reader status is unpredictable. For example, the reported node type may be different from the actual node type of the current node. You can use the ReadState attribute to check whether the reader is in an error state.

 

1. Follow these steps to use the XmlReader class:

(1) Use Create () of the XmlReader class to Create an instance of the class and pass in the read XML file name as a parameter.

(2) create a loop of Read () methods that are repeatedly called. This method starts from the first node of the file and then reads all the remaining nodes, but only one node is read for each call. If one node can be read, True is returned, returns False when the file is reached.

(3) In this loop, the attributes and methods of the XmlReader object will be checked to obtain information about the current node (type, name, data, and so on), and The Read () will be continuously executed () returns False.

 

(1) Start reading documents

To start reading xml documents, you can call any Read () method,XmlReader XmlTextReader, XmlNodeReader, and XmlValidatingReader are as follows:

XmlReader reader = XmlReader. Create ("Employees. xml ");
Reader. ReadStartElement ();

Or reader. MoveToContent () directly jumps to the document content. If the current node is not a content node (the content node is CDATA, Element, Entity, EntityReference). If the node is located on the property, the Element containing the property is returned.

[Important ]:

Although Microsoft. NET Framework includesXmlReaderClass implementation, such as XmlTextReader, XmlNodeReader, and XmlValidatingReader. However, in version 2.0, we recommend that you use the Create method to CreateXmlReaderInstance.

 

(2) reading Elements

(1) Read (), ReadString (), ReadStartElement (), and ReadEndElement () can all Read Element nodes.

(2) Each method is adjusted to the next node of the document.

(3) MovetoElement () only moves to the next node without reading it.

When XmlReader reads a document, its status may be as follows:

Member name Description
Closed The Close method has been called.
EndOfFile The end of the file is successfully reached.
Error If an error occurs, the read operation is blocked.
Initial Not calledReadMethod.
Interactive CalledReadMethod. Other methods may be called for the reader.
  (3) read attributesYou should first use HasAttributes to check whether there are any attributes. Then you can access the attributes through MoveToAttribute (), MoveToFirstAttribute (), and MoveToNextAttribute (). XmlReader reader = XmlReader. Create ("Employees. xml ");
If (reader. HasAttributes)
{
Reader. MoveToAttribute ("id ");
}

(4) Reading content and other data

ReadString () reads the content of the current node as a string. You can also use ReadElementContentAsXXX (). ReadContentAsXXX can read the text content at the current position.

This method returns the content, text, blank, important blank, or CDATA node of the element.

If it is located on the element,ReadStringConcatenates all text, important white spaces, white spaces, and CDATA node points, and then returns the data that is concatenated as element content. It stops when it encounters any mark (including comments and processing instructions. This can happen in the mixed content model or when the element end mark is read.

If it is located on the element text nodeReadStringExecute the same concatenation, that is, from the text node to the end mark of the element. If the reader is located on the attribute text nodeReadStringThe function is the same as that when the reader locates on the element start mark. It returns all element text nodes that are concatenated.

If it is located on the propertyReadStringReturns an empty string and moves the reader back to the element with this attribute.

If you callReadStringIt returns an empty string and locates the reader on the next node.

 

For [instance], see the next 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.