Programming with LINQ to XML for Objects (LINQ to XSD), Page 3
Writing VB Code Using LINQ to XML for Objects
So, now you have an XML document and schema. After the preceding section, you also have code generated, strongly typed classes. Load the XML document and query away.
To explore the LINQ to XSD generated Zoo and Zoos classes, follow these steps:
- Add a VB console application containing the C# class library to the solution.
- To the new project, add a reference to Microsoft.Xml.Scheam.Linq.dll by selecting Project|Add Reference, clicking the Browse tab, and navigating to the C:\Program Files\LINQ to XSD Preview\Bin folder.
- Add a reference to the GeneratedCode class library project.
- In the default Module1 file, add an Imports statement for both Microsoft.Xml.Schema.Linq and tempura.org.ZooSchema.xsd.
- Add the code in Listing 3.
Imports System.Xml Imports System.Xml.Linq Imports System.Linq Imports tempuri.org.ZooSchema.xsd Imports Microsoft.Xml.Schema.Linq Module Module1 Sub Main() Dim z = Zoos.Load("../../../GeneratedCode/Zoos.xml") Dim selectedZoos = From one In z.Zoo _ Select one For Each aZoo In selectedZoos Console.WriteLine(aZoo.Name) Next Console.ReadLine() End Sub End Module
Basic LINQ is pretty easy. The From clause comes first because this aids Intellisense. The Select clause comes last; this section creates what is called a projection. A projection can be a new typed by selectively picking fields from elements defined by the From clause or can be the items specified in the From clause.
The code in Listing 3 reads the XML document and loads all of the data into the anonymous type defined by the variable z. The variable z is actually an instance of Zoos. The next statement selects all zoos and the ForEach statement displays the zoo name in the console. That's it. Very object oriented and very easy.
LINQ is an extraordinarily powerful technology. For more on LINQ, check back with this column or pick up a copy of my book LINQ Unleashed for C# from Sams, available August 2008 (or pre-order today at Amazon.com).
This article culminates in the objectification of an XML document, which is subsequently queried with a single line of code. That's power. Underneath, there are so many cool and powerful technologies that it's amazing.
If you have been following along with my articles, you have probably guessed that to support querying an XML document with a single statement there is the .NET framework, XML and XSD, LINQ, anonymous types, the CodeDOM, extension methods, and generics. Each of these capabilities is powerful in its own right. When orchestrated toward a single purpose, LINQ to XSD, they let you take something a little complicated and advanced and manipulate it with ease.
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book LINQ Unleashed for C#; pre-order your copy today at Amazon.com. Paul Kimmel is an Application Architect for EDS. You may contact him for technology questions at firstname.lastname@example.org.
Lansing is having a free Day of .NET training at Lansing Community College on June 21st. Check out the web site for details. The group likes to think of it as a poor man's TechEd (because it's free), but the content will be excellent.
Copyright © 2008 by Paul T. Kimmel. All Rights Reserved.
Page 3 of 3