xml - VB.NET 中的 LINQ to XML

标签 xml vb.net linq linq-to-xml

我基本上是 LINQ 的新手。我在这里看了很多地方,很困惑。我看过一些示例,这些示例允许我使用 LINQ 强类型对象,但我并不真正理解它们,因为它们是在 C# 中,我想这可以让您使用 LINQ 做不同的事情(我认为?)。

无论如何,这就是我想要做的:

Dim productXML As XDocument = XDocument.Load( _
    Server.MapPath("~/App_Data/products.xml"))

Dim products As List(Of Product) = 'some query to select all products ?'

'set up Product properties here'
someProduct.ProductID = 'somehow get productid from XML'

编辑 - 我只想从 XML 文档中获取所有产品的列表并将它们放入泛型列表中。

最佳答案

Marc 是对的,VB 可以让您做很多好事。我自己是一个 C# 人,但我只是敲了一个 VB 解决方案,看看如何为你做。我已经在下面发布了代码并解释了关键部分。 VB 为 Xml 提供的功能给我留下了深刻的印象!

我在您的代码示例中看到您已经设法将 Xml 加载到 XDocument 中。完成 XDocument.Load 后,您可以使用一些特殊语法访问 Xml 文档。

对于初学者来说,我们想要从文档中获取所有产品;即所有 < Product > 元素。我们需要做以下事情:

Dim products = productsDoc...<Product>

这表示您需要文档中的所有 元素。这为我们提供了 XElement 的 IEnumerable 集合。

从集合中提取单个产品后,我们将希望访问产品的名称或价格等值。为此,我们需要执行以下操作:

' this gets the value of the price element within a product
product.<Price>.Value

这是一个完整的示例以及供您查看的预期输出:

Module Module1

    ' some products xml to use for this example
    Dim productsXml = <Xml>
                          <Product>
                              <Name>Mountain Bike</Name>
                              <Price>59.99</Price>
                          </Product>
                          <Product>
                              <Name>Arsenal Football</Name>
                              <Price>9.99</Price>
                          </Product>
                          <Product>
                              <Name>Formula One Cap</Name>
                              <Price>14.99</Price>
                          </Product>
                          <Product>
                              <Name>Robin Hood Bow</Name>
                              <Price>8.99</Price>
                          </Product>
                      </Xml>

    Sub Main()

        ' load the xml into an XDocument
        ' NOTE: this line isn't needed when using inline XML as per this example, 
        ' but I wanted to make this code easy to modify for reading in text files
        Dim productsDoc = System.Xml.Linq.XDocument.Parse(productsXml.ToString())

        ' get all <Product> elements from the XDocument
        Dim products = From product In productsDoc...<Product> _
                       Select product

        ' go through each product
        For Each product In products
            ' output the value of the <Name> element within product
            Console.WriteLine("Product name is {0}", product.<Name>.Value)
            ' output the value of the <Price> element within product
            Console.WriteLine("Product price is {0}", product.<Price>.Value)
        Next

    End Sub

End Module

程序输出为:

Product name is Mountain Bike
Product price is 59.99
Product name is Arsenal Football
Product price is 9.99
Product name is Formula One Cap
Product price is 14.99
Product name is Robin Hood Bow
Product price is 8.99

希望对您有所帮助。如果您想了解更多信息,请直接询问 :-)

很难在睡前写出连贯的东西! :-)

关于xml - VB.NET 中的 LINQ to XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1066213/

相关文章:

c# - Entity Framework 错误,一张表总是超时

c# - 获取项目索引(rownum)

java - 使用 JAXB 读取 XML 文件

java - 生成依赖于java参数的html代码

C# linq 联合问题

c# - 需要使用 asp.net 在下拉列表中显示名称和图像

php - 将 html 页面转换为 pdf

java - 从 XML 文件生成 Java 类的在线资源

c# - 读取 XML 文件时出错

.net - 在 .net 4.5 中并行化 for 循环