简体   繁体   English

VB.net读取XML元素

[英]VB.net read XML element

I am trying to write an application in VB.net but I stuck and I spend some time searching how to do this but can't get it. 我试图在VB.net中编写一个应用程序,但我坚持了下来,花了一些时间搜索如何执行此操作,但无法获取。 What i need is read specific xml element and write it in to the specific label. 我需要的是读取特定的xml元素并将其写入特定的标签。 If someone can give me an example. 如果有人可以给我一个例子。

I need output like this: 我需要这样的输出:

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

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

--etc--

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

What i need is something like this: Image 我需要的是这样的: 图片

Now is showing only first book. 现在只显示第一本书。 I need to loop all books. 我需要循环所有书。

Thank you 谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM