Sreedhar
Sreedhar

Reputation: 30035

linq to xml (c# to vb.net conversion)

What is the VB.net syntax below for?

   var list = xd.Descendants("product")
   .Select(element =>new 
   { 
      Title = element.Attribute("title").Value,                   
      Duration = element.Element("duration").Value 
   }).ToList(); 

Upvotes: 2

Views: 917

Answers (2)

Denis Troller
Denis Troller

Reputation: 7501

If you are using VB, there is some syntactic sugar for this:

Dim list = 
   From element In xd...<product>
   Select New With { _ 
       .Title = element.@title, _
       .Duration = element.<duration>.Value }

The nice part is that, if you have an xsd for your document (and you can create one through visual studio by inferring it from one or several xml documents), you can import it almost as you would a namespace and Visual Studio will give you intellisense completion when writing your query.

Some references:

Upvotes: 2

Keith
Keith

Reputation: 155792

Try this:

Dim list = 
   From element In xd.Descendants("product")
   Select New With { _ 
       .Title = element.Attribute("title").Value, _
       .Duration = element.Element("duration").Value }

You don't need to use Linq syntax, you can just use the underlying extensions:

Dim list = xd.Descendants("product"). _
    Select(Function(element) _ 
        New With { _ 
           .Title = element.Attribute("title").Value, _
           .Duration = element.Element("duration").Value _
        }). _
    ToList()

Upvotes: 1

Related Questions