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.