虎克的博客

Enthusiasm Biogeography-Biodiversity Informatics-Data Sciences

XML学习笔记2读取元素属性和内容值

| Comments

示例XML文件:

<?xml version="1.0" encoding="utf-8" ?>

<!— This is list of employees —>

<employees>

<employee employeeid="1">

<firstname>Nancy</firstname>

<lastname>Davolio</lastname>

<homephone>(206) 555-9857</homephone>

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

<homephone>(206) 555-9482</homephone>

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

<homephone>(206) 555-3412</homephone>

<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、在XML文档的元素间移动(Moving Between Elements)

五个基本方法:

ReadSubTree()方法:读取当前节点的子节点并返回XMLReader实例的一个子树。当你解析一个较大的XML文件,但是只希望处理其中的少部分数据的时候,这个方法非常有效。

作用示意如下图:

2009-05-09_145042

ReadToDescendant():

2009-05-09_145343

ReadToFollowing()

ReadToNextSibling()

2009-05-09_145457 XML

« XML文档的读写 XML学习笔记(三):写入XML文档的XmlTextWriter »

Comments