April 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Programming with LINQ to XML for Objects (LINQ to XSD)

  • June 20, 2008
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Introduction

It is not is hardly worth talking about; well, okay, just one more time. It seems there is an impression that advanced techniques are adopted by VB developers more slowly. Consequently, some things show up in C# first and VB later (if at all; remember anonymous delegates never showed up in VB). It is what it is. If it happens, mix C# in with your VB code rather than go without.

Language INtegrated Query (LINQ, pronounced "link") is a new technology in .NET 3.5 that pretty much lets you use one query language (LINQ, which is a lot like SQL) to write queries against SQL, Objects, XML, and pretty much anything you'd like. By implementing an IQueryProvider, as demonstrated in my upcoming book LINQ Unleashed for C# from Sams, you even can use LINQ to query things such as Active Directory or SharePoint.

LINQ is out and ready for prime time. LINQ to XSD is an extension of the core LINQ technologies and is at present still in alpha. LINQ to XSD builds on LINQ to XML. By including an XML Schema Document, LINQ to XSD will code generate strongly typed wrapper classes, permitting you to read and write the XML document as easily as you would create and use any objects. One minor drawback (for now) is that you will need to use a C# class library for the LINQ to XSD technology at the time this article was written. However, because everything in the C# class library is code generated, you don't need to know C# to use LINQ to XSD with your VB applications.

Creating the Class Library

When new kinds of projects are added to Visual Studio, they are actually added as stubs and wizard launching files. These stubs and launching files show up as icons in the Add New Project and Add New Items dialogs. Underneath, they are actually integrated through a variety of text files, JScript, and a general wizard that uses these elements to figure out what to do. That really doesn't matter. What matters is that these files have to exist for a given language for you to be able to use these new elements easily. Without them, you have to figure out all of the plumbing manually.

Tip: Microsoft has a new download portal at http://www.codeplex.com. You can get access to a lot of cool open source bits.

With the Alpha 0.2 preview version of LINQ to XSD, there are wizards for C# only. Hence, to use the technology at this point the easiest way is to create a C# class library project. Assuming that you have downloaded and installed the LINQ to XSD preview from http://www.microsoft.com/downloads/, follow these steps to create the C# LINQ to XSD Class Library:

  1. Start Visual Studio .NET 2008.
  2. Select File|New Pew Project.
  3. Navigate to the Visual C#|LINQ to XSD Preview project types.
  4. Click on the LINQ to XSD Library template.
  5. Name the class library generated code.
  6. Click OK

Next, you will need to define an XML document and the schema (.XSD) document. And, there are a couple more steps; these are covered in the next couple of sub-sections.

Defining an XML Document

When you create the XSD Preview project in the last section, a new assembly was added to the references list: Microsoft.Xml.Schema.Linq.dll. This assembly contains the code generators that will create wrapper classes for the XML and XSD documents.

Listing 1 contains an XML document that references an XSD document (you will create this in the next section). The XML document (named Zoos.xml) contains some of the zoos of the world.

Listing 1: An XML document containing zoos of the world.

<?xml version="1.0" encoding="utf-8" ?>
<Zoos xmlns="http://tempuri.org/ZooSchema.xsd">
   <Zoo>
      <Name>Abilene Zoo</Name>
      <City>Abilene</City>
      <State>Texas</State>
      <Country>USA</Country>
   </Zoo>
   <Zoo>
      <Name>Asa Zoological Park</Name>
      <City>Hiroshima</City>
      <State></State>
      <Country>Japan</Country>
   </Zoo>
   <Zoo>
      <Name>Assiniboine Park Zoo</Name>
      <City>Winnipeg</City>
      <State>Manitoba</State>
      <Country>Canada</Country>
   </Zoo>
   <Zoo>
      <Name>Belize Zoo</Name>
      <City>Belize City</City>
      <State></State>
      <Country>Belize</Country>
   </Zoo>
   <Zoo>
      <Name>Birmingham Zoo</Name>
      <City>Birmingham</City>
      <State>Alabama</State>
      <Country>USA</Country>
   </Zoo>
   <Zoo>
      <Name>Brevard Zoo</Name>
      <City>Melbourne</City>
      <State>Florida</State>
      <Country>USA</Country>
   </Zoo>
   <Zoo>
      <Name>The Bronx Zoo</Name>
      <City>New York</City>
      <State>New York</State>
      <Country>USA</Country>
   </Zoo>
   <Zoo>
      <Name>Buffalo Zoological Gardens</Name>
      <City>Buffalo</City>
      <State>New York</State>
      <Country>USA</Country>
   </Zoo>
   <Zoo>
      <Name>Parc Zoologique de Lille</Name>
       <City>Lille</City>
      <State></State>
      <Country>France</Country>
   </Zoo>
   <Zoo>
      <Name>Parken Zoo</Name>
      <City>Eskilstuna</City>
      <State></State>
      <Country>Sweden</Country>
   </Zoo>
   <Zoo>
      <Name>San Diego Wild Animal Park</Name>
      <City>Escondido</City>
      <State>California</State>
      <Country>USA</Country>
   </Zoo>
   <Zoo>
      <Name>Zoo Augsburg</Name>
      <City>Augsburg</City>
      <State></State>
      <Country>Germany</Country>
   </Zoo>
   <Zoo>
      <Name>Zoological de Culiacan</Name>
      <City>Culiacan</City>
      <State></State>
      <Country>Mexico</Country>
   </Zoo>
</Zoos>

For all intents and purposes, the XML document in Listing 1 is a persisted collection of Zoo objects, and that is precisely how you want to treat them in your code.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel