使用xElement循环遍历XML记录

时间:2013-03-02 23:26:52

标签: vb.net visual-studio-2010

我有以下XML

     <xml>
         <version>1.0</version>
         <products>
              <product>
                    <name>Product 1</ProductName>
                    <id>1</ProductId>
              </product>
              <product>
                    <name>Product 2</ProductName>
                    <id>2</ProductId>
              </product>
              <product>
                    <name>Product 3</ProductName>
                    <id>3</ProductId>
              </product>
          </products>
      </xml>

我有以下代码来循环产品

   Dim xelement As XElement = xelement.Load("aaa.xml")
   Dim products As IEnumerable(Of XElement) = xelement.Elements()
       For Each product In products
           Console.WriteLine(product.Elements("name").Value)
       Next product

但是product.Elements("name").Value给出了错误。我在这里做错了什么?

3 个答案:

答案 0 :(得分:2)

问题不在于XML元素名称(尽管您的编辑包含无效的XML - 您只更改了开始标记,而不是结束标记)。

问题是产品包含您加载的XML文档的子元素 - 在本例中为<Version><Products>....</Products>(即所有产品标记及其子代)。

您要做的是抓取所有<name>节点的集合,您可以通过以下两种方式之一完成此操作:

Dim products As IEnumerable(Of XElement) = xelement.Element("products").Element("product").Element("name")

Dim products As IEnumerable(Of XElement) = xelement.Descendants("name")

然后,在For Each中,只需在Value上致电Product,就像这样:

For Each product As XElement In products
    Console.WriteLine(product.Value)
Next

输出

Product 1
Product 2
Product 3

已编辑以添加完整代码

Imports System.Xml.Linq
Imports System.Collections.Generic

Module Module1

    Sub Main()

        Dim Xml As String = "<xml><version>1.0</version><products><product><name>Product 1</name><id>1</id></product><product><name>Product 2</name><id>2</id></product><product><name>Product 3</name><id>3</id></product></products></xml>"

        Dim xelement As XElement = xelement.Parse(Xml)

        Dim products As IEnumerable(Of XElement) = xelement.Descendants("name")

        For Each product As XElement In products
            Console.WriteLine(product.Value)
        Next

        Console.ReadLine()
    End Sub

End Module

答案 1 :(得分:2)

问题是Name包含您加载的XML文档的子元素 - 在本例中为<Version><name>....</name>(即所有名称标记及其子代)。

您要做的是获取所有<Name>节点和<tel>节点的集合,您可以这样做:

 Dim Names As IEnumerable(Of XElement) = xelement.Descendants("Name")

  For Each Name As XElement In Names 
        MsgBox((Name.Value)
  Next

你可以为“电话”做同样的事情

完整代码:

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

    Dim cbFileName As String = "<xml><version>1.0</version><name>A1</name><tel>12</tel><name>A2</name><tel>21</tel><name>A3</name><tel>11</tel></xml>"

    Dim xelement As XElement = xelement.Load(cbFileName.Text)

    Dim products As IEnumerable(Of XElement) = xelement.Descendants("name")

    For Each product As XElement In products
        MessageBox.Show(product.Value)
    Next

    Dim Telephone As IEnumerable(Of XElement) = xelement.Descendants("tel")

    For Each telep As XElement In Telephone
        MessageBox.Show(telep.Value)
    Next

End Sub

<强>输出:

A1 A2 A3

12 21 11

答案 2 :(得分:0)

product.Elements("name")返回IEnumerable<XElement>,因此没有.Value属性。试试这个:

Dim products As IEnumerable(Of XElement) = xelement.Elements()
For Each product In products
    Console.WriteLine(product.Element("name").Value)
Next product