VB.NET LINQ - Query Simple XML

May 17, 2018 Oclemy VB.NET LINQ, VB.NET XML 6 minutes, 9 seconds

VB.NET LINQ - Query Simple XML

LINQ is a technology that allows querying of data as a first class language feature.

Programming Languages have generally not incorporated the XML-like syntax in natively.

However, LINQ was a game changer in this regards and is one those features that make .NET powerful yet easier to program in.

Not only can we query collections like lists in LINQ but also databases and XML.

In this class we want to look at how to query a simple XML document in LINQ in VB.NET.

Let's look at an example to query xml file containing spaceshuttle names and their propellant.

We want to obtain those spacecrafts using the Chemical propellant and show them in a console.

We are Building a Vibrant YouTube Community

We have a fast rising YouTube Channel of friends. So far we've accumulated more than 2.6 million agreggate views and more than 10,000 subscribers. Here's the Channel: ProgrammingWizards TV.

Please go ahead subscribe(free obviously) as well. If you have a question or a comment you can post there instead of in this site.People are suggesting us tutorials to do there so you can too.

Here's this tutorial in Video Format.

Let's start.


What is XML?

In full XML stands for Extensible Markup Language.

It is a simple, very flexible text format derived from Standard Generalized Markup Language(SGML).

XML is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.

As a textual data format XML has a strong support via Unicode for different human languages.

It was first conceived as a way of tackling the then challenges of large-scale electronic publishing.

However, due it's very flexible nature, XML is nowadays playing an important role in the exchange of a wide variety of data on the Web and elsewhere.

This is because it allows us easily represent arbitrary data structures, hence can easily be used in web services.

The W3C's XML 1.0 Specification and several other related specifications, all of them free and open standards, define XML.

Some of the popular formats that have been developed with the basis as XML include:

  1. XHTML.
  2. RSS.
  3. Atom.
  4. SOAP.
  5. SVG. and hundreds other formats.

More details on XML can be found here.

Commonly Used XML terminologies

Here are some fo the commonly used terminologies when it comes to XML.

Character

Characters are the basic constituents of an XML document. The Characters strung together are what make up the XML document.

Almost every legal Unicode character may appear in an XML document.

Tag

In XML a tag is a markup construct that begins with < and ends with >.

These tags do come in three flavors:

  1. start-tag, such as <section>;
  2. end-tag, such as </section>;
  3. empty-element tag, such as <line-break />.

Element

An element is a logical document component that either begins with a start-tag and ends with a matching end-tag or consists only of an empty-element tag.

Elements normally have some content. This content is the charaters between the start and end tags.

Here's a simple element:

<name>Challenger</name>

Elements can be nested to contain child elements.

And here's a nested XML element:

                <spaceshuttle>
                    <name>Atlantis</name>
                    <propellant>Plasma Ions</propellant>
                </spaceshuttle>

Attribute

This is a markup construct consisting of a name–value pair that exists within a start-tag or empty-element tag.

Here's an example:

<img src="mimi.png" alt="Mimi" />

In this case the we have two attributes with names: src and alt. Their values on the other are mimi.png and Mimi respectively.

Declaring an XML Document

XML documents may begin with an XML declaration that describes some information about themselves.

An example is <?xml version="1.0" encoding="UTF-8"?>.

Normally XML documents comprise entirely of characters from the Unicode repertoire.

Unicode itself defines encodings that cover the entire repertoire; well-known ones include UTF-8 and UTF-16.


Module1.vb

Here's the full code.

Imports System
Imports System.Linq
Imports System.Xml.Linq
Module Module1

    Sub Main()

        Dim chemicalPoweredSpaceshuttles = From spaceshuttle In getXML().Elements("spaceshuttle")
                                           Where spaceshuttle.Element("propellant") = "Chemical"
                                           Select spaceshuttle.Element("name")
        For Each name In chemicalPoweredSpaceshuttles
            Console.WriteLine(name.Value)
        Next
        Console.ReadLine()

    End Sub

    Function getXML() As XElement
        Dim spaceshuttles As XElement =
            <spaceshuttles>
                <spaceshuttle>
                    <name>Columbia</name>
                    <propellant>Chemical</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Atlantis</name>
                    <propellant>Plasma Ions</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Challenger</name>
                    <propellant>Nuclear</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Endeavor</name>
                    <propellant>Chemical</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Voyager A</name>
                    <propellant>Warp Drive</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Voyager B</name>
                    <propellant>AntiMatter</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Spitzer</name>
                    <propellant>AntiMatter</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Apollo</name>
                    <propellant>Chemical</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>WMAP</name>
                    <propellant>Laser Beam</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Swift</name>
                    <propellant>Plasma Ions</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>New Horizon</name>
                    <propellant>AntiMatter</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Pioneer</name>
                    <propellant>Chemical</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Juno</name>
                    <propellant>Nuclear</propellant>
                </spaceshuttle>
                <spaceshuttle>
                    <name>Casini</name>
                    <propellant>Laser Beam</propellant>
                </spaceshuttle>

            </spaceshuttles>
        Return spaceshuttles
    End Function

End Module

Result

Columbia
Endeavor
Apollo
Pioneer

Explanations

Clearly you can see we have easily queried XML data and obtained a subset we are interested in.

First we added three namespaces we needed for our project:

Imports System
Imports System.Linq
Imports System.Xml.Linq

Then defined our module to contain our code:

Module Module1
'''
End Module

We then created a Function called getXML() that retruns data of type System.Xml.Linq.XElement:

Function getXML() As XElement

End Function

XElement basically represents for us an XML element.

Then we created the XML data and loaded into object of type XElement and returned it. VB.NET 2008 introduced XML literals so that we can just use XML code literally in our VB.NET code.

Inside the Main() subroutine, first we created data from our XML using LINQ:

Dim chemicalPoweredSpaceshuttles = From spaceshuttle In getXML().Elements("spaceshuttle")
                                           Where spaceshuttle.Element("propellant") = "Chemical"
                                           Select spaceshuttle.Element("name"

We then looped through the data and showed it in the console:

For Each name In chemicalPoweredSpaceshuttles
            Console.WriteLine(name.Value)
        Next

And that's it, we've looked at our first LINQ to XML VB.NET tutorial.

Running the Code

Just copy the code and paste into your VB.NET file and run.

Best regards.

Comments