LINQ VB.NET中的XML(LINQ to XML in VB.NET)

2019-06-24 14:45发布

我基本上是全新的,以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文档中的产品列表,并把它们放到一个泛型列表。

Answer 1:

马克是正确的,VB可以让你做很多不错的东西。 我是一个C#的家伙自己,但我只是敲了一个VB的解决方案,看看如何为你做它。 我已经发布了下面的代码,并解释了关键部位。 我印象非常深刻的特点VB具有对XML!

我看到你已经成功地将XML加载到一个XDocument你的代码样本。 一旦你完成了你的XDocument.Load你可以使用一些特殊的语法访问XML文档。

对于初学者来说,我们希望获得所有从文档中的产品; 即,所有<产品>元素。 我们需要做到以下几点:

Dim products = productsDoc...<Product>

这是说你想从文档中所有的<产品>元素。 这给了我们XElements的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

希望这有用。 如果您想更多的信息,请只问:-)

这是很难写的东西在睡前一致! :-)



Answer 2:

琼斯博士发布了一个很好的例子!

要获得命名类型对象的集合,而不是匿名类型的对象(两者都强类型)做到这一点:

Module Module1

' some products xml to use for this example '
    Dim productsXml As XElement = _
    <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>

Class Product

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _price As Double
    Public Property Price() As Double
        Get
            Return _price
        End Get
        Set(ByVal value As Double)
            _price = value
        End Set
    End Property

End Class

Sub Main()

    ' get an IEnumerable of Product objects '
    Dim products = From prod In productsXml...<Product> _
                   Select New Product With {.Name = prod.<Name>.Value, .Price = prod.<Price>.Value}

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

End Sub

End Module


Answer 3:

OK,这个怎么样?

Dim productXML As XDocument = XDocument.Load( _    
    Server.MapPath("~/App_Data/products.xml"))    
'
For Each product as Product In productXML.Document.Elements("Product")
    'do something with each product
Next


Answer 4:

我可能有点晚了这里的聚会,但我不能相信没有人提供了XmlSerializer选项:

Public Class Product
    Property Description As String
    Property Price As Double

    Public Shared Function FromXml(serverPath As String) As IEnumerable(Of Product)

       Using fs = IO.File.OpenRead(serverPath)
           Dim p As New Product
           Return New XmlSerializer(p.GetType).Deserialize(fs)
       End Using

    End Function

End Class

然后,您可以返回一个IEnumerable产品从共享功能:

dim listOfProducts = Product.FromXml(Server.MapPath("~/App_Data/products.xml"))

这不使用LinqToXml因为如此,但它反序列化XML产品的IEnumerable,您可以照常使用Linq。



文章来源: LINQ to XML in VB.NET