vb net:从xml获取字符串,如何获取三个值?

时间:2012-10-23 20:49:48

标签: vb.net

我得到了流派价值"戏剧"但它只有一个可以得到,我怎么能得到三个类型的价值,如"戏剧","喜剧"和"惊悚" ??

<details>
    <id>734357</id>
    <title>vb best</title>
    <year>2012-07-27</year>
    <genre>Drama</genre>
    <genre>Comedy</genre>
    <genre>Thriller</genre
    <studio></studio>
</details>


    Dim doc As New XmlDocument()
    Dim nodes As XmlNodeList
    doc.Load(FILE_NAME)

    nodes = doc.SelectNodes("/details")
    Dim node As XmlNode

    For Each node In nodes

        Dim nodeid As XmlNode = node.SelectSingleNode("id")
        If nodeid IsNot Nothing Then
            MsgBox(node.SelectSingleNode("id").InnerText)
        End If
        Dim nodeimdb_id As XmlNode = node.SelectSingleNode("title")
        If nodeimdb_id IsNot Nothing Then
            MsgBox(node.SelectSingleNode("title").InnerText)
        End If

        Dim nodegenre As XmlNode = node.SelectSingleNode("genre")
        If nodegenre IsNot Nothing Then
            MsgBox(node.SelectSingleNode("genre").InnerText)
        End If

    Next

2 个答案:

答案 0 :(得分:0)

Dim doc As New XmlDocument()
Dim nodes As XmlNodeList
doc.Load(FILE_NAME)

nodes = doc.SelectNodes("/details")
Dim node As XmlNode

For Each node In nodes

    Dim nodeid As XmlNode = node.SelectSingleNode("id")
    If nodeid IsNot Nothing Then
        MsgBox(nodeid.InnerText)
    End If
    Dim nodeimdb_id As XmlNode = node.SelectSingleNode("title")
    If nodeimdb_id IsNot Nothing Then
        MsgBox(nodeimdb_id.InnerText)
    End If

    Dim genreNodes As XmlNodeList = node.SelectNodes("genre")
    For each genreNode in genreNodes
        MsgBox(genreNode.InnerText)
    Next

Next

将是一种方式。 注意,看到你已经有了节点,不需要再次获取它。

答案 1 :(得分:0)

使用.SelectNodes代替.SelectSingleNode