vb.net 使用 xElement 循环遍历 XML 记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15180623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:35:25  来源:igfitidea点击:

Looping through XML records using xElement

vb.netvisual-studio-2010

提问by Khawer Zeshan

I have the following XML

我有以下 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>

And i have the following code for looping through the products

我有以下代码用于遍历产品

   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

but product.Elements("name").Valueis giving error. What i am doing wrong here?

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

采纳答案by Tim

The issue is not with the XML element names (although your edit has invalid XML - you only changed the opening tag, not the closing tag).

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

The issue is that products contains the child elements of the XML document you loaded - in this case, <Version>and <Products>....</Products>(i.e., the all the products tags and their children).

问题是产品包含您加载的 XML 文档的子元素 - 在这种情况下,<Version><Products>....</Products>(即所有产品标签及其子元素)。

What you want to do is grab a collection of all the <name>nodes, and you can do this one of two ways:

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

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

or

或者

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

Then, in your For Each, simply call Valueon Product, like this:

然后,在你的For Each,只需拨打ValueProduct,就像这样:

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

The output will be

输出将是

Product 1
Product 2
Product 3

EDITED to add complete code

编辑添加完整的代码

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

回答by Tithi Patel

The issue is that Name contains the child elements of the XML document you loaded - in this case, <Version>and <name>....</name>(i.e., the all the name tags and their children).

问题在于 Name 包含您加载的 XML 文档的子元素 - 在这种情况下,<Version>以及<name>....</name>(即所有名称标签及其子元素)。

What you want to do is grab a collection of all the <Name>nodes and <tel>nodes, and you can do on this way:

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

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

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

Same you can do for "Tel"

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

Complete code:

完整代码:

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

Output:

输出:

A1 A2 A3

A1 A2 A3

12 21 11

12 21 11

回答by MarcinJuraszek

product.Elements("name")return IEnumerable<XElement>, so it has no .Valueproperty. Try this one:

product.Elements("name")return IEnumerable<XElement>,所以它没有.Value属性。试试这个:

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