VB.net读取XML元素

时间:2014-11-19 11:38:16

标签: xml vb.net

我正在尝试在VB.net中编写一个应用程序,但我卡住了,我花了一些时间来搜索如何做到这一点,但无法得到它。 我需要的是读取特定的xml元素并将其写入特定标签。如果有人可以给我一个例子。

我需要这样的输出:

--Book 1---
Title: Everyday Italian
Author: Giada De Laurentiis

--Book 2--
Title: Harry Potter
Author: J K. Rowling

--etc--

XML:

<bookstore>
<book>
    <title>Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
</book>
<book>
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
</book>
<book>
    <title>XQuery Kick Start</title>
    <author>James McGovern</author>
    <year>2003</year>
    <price>49.99</price>
</book>
<book>
    <title>Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
</book>

我需要的是这样的: Image

现在只显示第一本书。我需要循环所有书籍。

谢谢

1 个答案:

答案 0 :(得分:2)

Dim xmlRoot As XElement = XDocument.Load("x:\books.xml").Root

For Each book As XElement In xmlRoot.<book>
     Debug.WriteLine(book.<title>.Value)
     Debug.WriteLine(book.<author>.Value)
     Debug.WriteLine(book.<year>.Value)
     Debug.WriteLine(book.<price>.Value)
Next
相关问题