LINQ to XML in VB.NET

前端 未结 4 1524
南方客
南方客 2020-12-10 06:07

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 do

4条回答
  •  伪装坚强ぢ
    2020-12-10 06:31

    I may be a little late to the party here, but I can't believe nobody has offered the XmlSerializer option:

    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
    

    You can then return an iEnumerable of Product from the shared function:

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

    This doesn't use LinqToXml as such, but it deserializes the xml to an iEnumerable of product, which you can use Linq on as usual.

提交回复
热议问题