VB.NET中的LINQ to XML [英] LINQ to XML in VB.NET

查看:62
本文介绍了VB.NET中的LINQ to XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是LINQ的新手.我在这里环顾了很多,很困惑.我看过一些示例,这些示例使我可以使用LINQ来增强类型对象,但由于它们位于C#中,因此我并不真正理解它们,我想它们可以使您使用LINQ做不同的事情(我认为吗?).

I'm basically brand new to LINQ. I've looked around a lot on here and am pretty confused. I've seen some examples that allow me to strong type objects using LINQ but I don't really understand them because they're in C#, which I guess lets you do different things with LINQ(I think?).

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

Anyways, this is what I'm trying to do:

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文档中获取所有产品的列表,然后将它们放入泛型"列表中.

EDIT - I just want to get a list of all the products from the XML doc and put them into a Generics list.

推荐答案

马克是正确的,VB可以让您做很多不错的事情.我本人是C#,但是我只是提出了一个VB解决方案,以了解如何为您做这件事.我在下面发布了代码,并解释了关键部分. VB为Xml提供的功能给我留下了深刻的印象!

Marc is right, VB lets you do lots of nice stuff. I'm a C# guy myself, but I just knocked up a VB solution to see how to do it for you. I've posted the code below and explained the key parts. I was very impressed with the features VB has for Xml!

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

I see in your code sample that you've already managed to load your Xml into an XDocument. Once you've done your XDocument.Load you can access the Xml document using some special syntax.

对于初学者,我们希望从文档中获取所有产品;即所有<产品>元素.我们需要执行以下操作:

For starters we want to get all the products from the document; i.e. all < Product > elements. We need to do the following:

Dim products = productsDoc...<Product>

这表示您要全部<产品>文档中的元素.这为我们提供了IEnumerable的XElements集合.

This says that you want all < Product > elements from the document. This gives us an IEnumerable collection of XElements.

一旦我们从集合中取出单个产品,我们将要访问该产品的值,例如其名称或价格.为此,我们需要执行以下操作:

Once we've pulled an individual product from the collection we'll want to access the product's values like it's name or price. To do that we need to do the following:

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

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

Here's a full example along with the expected output for you to look at:

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

我希望这会有所帮助.如果您需要更多信息,请询问:-)

I hope this has been helpful. If you'd like any more information please just ask :-)

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

It's hard to write something coherent at bedtime! :-)

这篇关于VB.NET中的LINQ to XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆