pSuper
pSuper

Reputation: 43

LINQ to XML Query in VB

Ok, Im new to LINQ to XML and need some help getting my query set up. I have googled for hours and it seems every diffrent website has a diffrent way of doing it and I am getting really really confused. Here is my XML:

<?xml version = "1.0"?>
<booking>
    <client>
        <id>0001</id>
        <name>Jack Loper</name>
        <numofseats>3</numofseats>
        <seats>C1C2C3</seats>
        <cost>30</cost>
    </client>
    <client>
        <id>0002</id>
        <name>Joe Bloggs</name>
        <numofseats>1</numofseats>
        <seats>D8</seats>
        <cost>10</cost>
    </client>
</booking>

Now, I have sucessfully got my document loaded into VB:

Dim BookingDatabase As XDocument
BookingDatabase = XDocument.Load(My.Application.Info.DirectoryPath & "\Booking_Database.xml")

I would like the output from my query to be:

0001 Jack Loper 0002 Joe Bloggs

How would I set up my query? At the moment I have this, but I have no idea what the hell I am actually doing:

Dim query = From ex In BookingDatabase.Descendants.Elements("client")
                    Select id = ex.Attribute("id").Value
        For Each ex In query
            Console.WriteLine(ex)
        Next

If you could explain each step, that would be nice. Thanks

Upvotes: 0

Views: 3111

Answers (1)

Tim
Tim

Reputation: 28520

You may have already figured out how to do this, but in case you didn't (or someone else in the future has the same problem and comes across your question in a search), there are a couple of ways to do it (syntactically), but the concept is the same.

You're on the right path with your query, except that you're looking for an attribute ("id") which is really on element. If it was an attribute it'd look something like this:

<client id="00001">

What you want to do is return a collection of anonymous types - each instance of the type will hold all the values you tell it to (in this case, id and name). First the code:

Dim query = From ex In BookingDatabase.Descendants("client")
            Select New With {
                .id = ex.Element("id").Value,
                .name = ex.Element("name").Value
            }

Or, alternatively:

Dim query = BookingDatabase.Descendants("client").Select(Function(ex) New With {.id = ex.Element("id").Value, .name = ex.Element("name").Value})

Both return the same thing, a collection of anonymous types that contain id and name.

So what's happening in these queries?

First, BookingDatabase.Descendants("client") is getting a collection of all theclient` nodes.

Next, 'Select New With` is creating an anonymous type, which will hold the values you select with this statement:

{
  .id = ex.Element("id").Value,
  .name = ex.Element("name").Value
}

The end result is that you get a collection of these anonymous types, which you can then iterate through like this:

For Each ex in Query
    Console.WriteLine(ex.id + " " + ex.name)
Next

Note that your code, as written, would display something like { id = 0001, name = Jack Loper } because you were simply using ex. With the anonymous types you get properties that you can use.

If you had a class defined already, say something like:

Public Class Clients

    Public Property ID As String
    Public Property Name As String

End Class

You could return a collection of typed objects with this:

Select New Clients With {
     .ID = ex.Element("id").Value,
     .Name = ex.Element("name").Value
}

Upvotes: 1

Related Questions