XML learning notes (2): Reading elements, attributes, and content values

Source: Internet
Author: User
Tags cdata

Example XML file:

<?xml version="1.0" encoding="utf-8" ?>
<!-- This is list of employees -->
<employees>
<employee employeeid="1">
<firstname>Nancy</firstname>
<lastname>Davolio</lastname>
<notes>
<![CDATA[includes a BA in psychology from Colorado State University in
1970. She also completed "The Art of the Cold Call." Nancy is a member of
Toastmasters International.]]>
</notes>
</employee>
<employee employeeid="2">
<firstname>Andrew</firstname>
<lastname>Fuller</lastname>
<notes>
<![CDATA[Andrew received his BTS commercial in 1974 and a Ph.D. in
international marketing from the University of Dallas in 1981. He is fluent
in French and Italian and reads German. He joined the company as a sales
representative, was promoted to sales manager in January 1992 and to vice
president of sales in March 1993. Andrew is a member of the Sales
Management Roundtable, the Seattle Chamber of Commerce, and the Pacific
Rim Importers Association.]]>
</notes>
</employee>
<employee employeeid="3">
<firstname>Janet</firstname>
<lastname>Leverling</lastname>
<notes>
<![CDATA[Janet has a BS degree in chemistry from Boston College (1984).
She has also completed a certificate program in food retailing management.
Janet was hired as a sales associate in 1991 and promoted to sales
representative in February 1992.]]>
</notes>
</employee>
</employees>

1. Move Elements Between XML documents (Moving Between Elements)

Five basic methods:

ReadSubTree () method: Read the subnode of the current node and return a subtree of the XMLReader instance. This method is very effective when you parse a large XML file but only want to process a small amount of data.

Function diagram:

ReadToDescendant ():

ReadToFollowing ()

ReadToNextSibling ()

Skip ()

2. Move Between Attributes (Moving Between Attributes)

Four main methods:

MoveToAttribute ()

MoveToFirstAttribute ()

MoveToNextAttribute ()

MoveToElement ()

Function diagram:

As you can see in above figure, if you call MoveToAttribute () by passing firstname as the parameter, the reader moves to the firstname attribute. when you are on the lastname attribute, calling MoveToFirst () will take the reader to the id attribute. calling MoveToNextAttribute () when the reader is on the id attribute will advance the reader to the firstname attribute. finally, calling MoveToElement () from any attribute will take the reader back to the <employee> element.

3. Read Data Reading Content

ReadInnerXml ()

ReadOuterXml ()

ReadString ()

Method explanation:

If the Reader is in the first <employee> element, the XML content returned by the ReadInnerXML () method is as follows:

<firstname>Nancy</firstname>
<lastname>Davolio</lastname>
<notes>
<![CDATA[includes a BA in psychology from Colorado State University in 1970. She
also completed "The Art of the Cold Call." Nancy is a member of Toastmasters
International.]]>
</notes>

The XML content returned by the ReaderOuterXML () method is as follows:

<employee employeeid="1">
<firstname>Nancy</firstname>
<lastname>Davolio</lastname>
<notes>
<![CDATA[includes a BA in psychology from Colorado State University in 1970. She
also completed "The Art of the Cold Call." Nancy is a member of Toastmasters
International.]]>
</notes>
</employee>

The ReadString () method reads only the content of one element or text node as a string.

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.