LINQ to XML Rocks Because by ThinqLinq

LINQ to XML Rocks Because

I’m in the process of finalizing a number of presentations on LINQ. I will be posting the demos and slides soon. As a teaser, here’s a quick snippet from the XLinq demo. I begin by showing the new functional construction syntax for XLINQ.

'Uses Functional Construction
Dim x As New XElement("LinqEssentials", _
New XElement("Category", "LINQ"), _
New XElement("Category", "DLINQ"), _
New XElement("Category", "XLINQ"))
Dim y As New XElement("LanguageFeatures")
y.Add(New XElement("release", New XAttribute("version", "2.0"), _
New XElement("feature", "Generics"), _
New XElement("feature", "Iterators"), _
New XElement("feature", "Anonymous Delegates"), _
New XElement("feature", "Nullable Types") _
))
y.Add(New XElement("release", New XAttribute("version", "3.0"), _
New XElement("feature", "Implicit Types"), _
New XElement("feature", "Extension methods"), _
New XElement("feature", "Lambda Expressions"), _
New XElement("feature", "Object initializers"), _
New XElement("feature", "Anonymous Types"), _
New XElement("feature", "Expression Trees") _
))
y.Add(New XElement("release", New XAttribute("version", "9.0"), _
New XElement("feature", "dynamic interfaces"), _
New XElement("feature", "dynamic identifiers"), _
New XElement("feature", "XML Literals") _
))
Dim z As New XElement("XLinqRocksBecause")
z.Add(x)
z.Add(y)

Debug.Write(z)

While it is elegant and lets you work more directly with the document structure, the c# method is not quite as cool as the VB one. In the demo, I run the first code snippet, then copy the output from the debug.write method which sends out the XML nicely formatted. Then you create a new variable and paste in the output from the debug.write call as follows:

Dim x = <VBXLinqRocksBecause onDate=<%= Today %>>
    <LinqEssentials>
        <Category>LINQ</Category> 
        <Category>DLINQ</Category> 
        <Category>XLINQ</Category> 
    </LinqEssentials> 
    <LanguageFeatures> 
        <release version="7.0"> 
            <feature>Generics</feature> 
            <feature>Iterators</feature> 
            <feature>Anonymous Delegates</feature> 
            <feature>Nullable Types</feature> 
        </release> 
        <release version="8.0"> 
            <feature>Implicit Types</feature> 
            <feature>Extension methods</feature> 
            <feature>Lambda Expressions</feature> 
            <feature>Object initializers</feature> 
            <feature>Anonymous Types</feature> 
            <feature>Expression Trees</feature> 
        </release> 
        <release version="9.0"> 
            <feature>dynamic interfaces</feature> 
            <feature>dynamic identifiers</feature> 
            <feature>XML Literals</feature> 
        </release> 
    </LanguageFeatures> 
</VBXLinqRocksBecause> 

Notice, there are no quotes around any of this. Accessing an element can be as simple as the following (note, you will need to turn Option Strict OFF for this one to work).:

Console.WriteLine(CStr(x.@onDate)

While I think LINQ and XLINQ are cool, I must applaud the VB/XML/DATA teams for their efforts on this one. Finally XML in VB ROCKS!

If you want to learn more, head on over to the LINQ center on MSDN, or come to my talks at the Alabama Code Camp, Atlanta Dotnet User Group, or Atlanta Code Camp over the next few weeks.

Posted on - Comment
Categories: Linq to XML - VB -
comments powered by Disqus