Click here to Skip to main content
15,610,563 members
Articles / Desktop Programming / XAML
Posted 6 Nov 2008


65 bookmarked


Rate me:
Please Sign up or sign in to vote.
4.75/5 (11 votes)
6 Nov 2008CPOL3 min read
How to use LINQ through XSD and XML


This article explains how to use Linq with XML using the XSD file.
The aim of using Linq to XSD is to enhance Linq to XML by having more features, strong typed fields,...

This article provides a way to use an XML database through the new Linq To XSD technology.
It does not enumerate all possibilities, all error cases..., but just gives the reader what is needed to start working.


Before reading this article, you must have some LINQ, XML and XSD bases. Please refer to following links if you're not familiar with those technologies:

Another LINQ to Something

This technology may be used to manage with XML database.

I've started searching information LINQ To XML to process queries other than my XML database. But I did not like the syntax for queries, because it is very blur and not strong typed. Finally code is not readable and may be hard to understand for beginners. I was searching for something very simple and easy to use.

In fact, I would like to write the simplest code (What else?) and manage my data without any effort! LINQ to XSD makes it for us! So you just need money, girls and whisky ;-)

Prerequisites and Warnings

Before creating a new project, you need to install the LINQ To XSD alpha 2 Refresh on your computer. It is an alpha software, so install it carefully.

The Database

To use Linq To XSD, you must have an XSD file which describes and validates your XML database schema.
In the sample zipped file, you will find a Database directory which includes an XML database with its associated XSD schema file.

XSD File

<xsd:schema xmlns:xsd=""

  <xsd:element name="books" type="bks:BooksForm"/>

  <xsd:complexType name="BooksForm">
      <xsd:element name="book"

  <xsd:complexType name="BookForm">
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="pub_date" type="xsd:date" />
      <xsd:element name="review"   type="xsd:string"/>
    <xsd:attribute name="id"   type="xsd:string"/>

XML Database File

<?xml version="1.0"?>
<x:books xmlns:x="urn:books">
   <book id="bk001">
      <author>Robert E. Howard</author>
      <title>The Coming of Conan the Cimmerian</title>
      <genre>Science Fiction & Fantasy</genre>
      <review>Conan let go, spat blood from his mouth..</review>

   <book id="bk002">
      <author>Terry Goodkind</author>
      <title>Wizard's First Rule</title>
      <review>My world contest best book</review>

   <book id="bk003">
      <author>HP Lovecraft</author>
      <title>Call of Cthulhu</title>
      <genre>Science Fiction & Fantasy</genre>
      <review>I am writing this under an appreciable mental strain, 
		since by tonight I shall be no more...</review>

Creating a New Project

After having installed Linq To XSD, just open Visual Studio 2008 and create a new Linq to XSD project.

Create project

Then add an XSD file to the project (either create a new one, or import an existing one).

Add XSD file

Finally, just change the build options for the XSD file. Open XSD file properties from the Solution Explorer, and change the Build action to LinqToXSDSchema.

Build action

Just build your project, and then you are now ready to use Linq with another XML file!! Amazing!!

Opening an XML Database

Note: If you have specified a namespace in XSD file, then the same namespace is available from C#, with all classes included!
In sample XSD/XML files, I used the namespace urn::books::.

urn.books.books m_books_database = urn.books.books.Load("books.xml");


Then, you may directly use the m_books_database object inside LINQ queries:

//retrieve the books list
var list_books = from c in
                 select c;

Now, the list_books contains the full list of books from your XML file. So you can manage data by using a foreach or sending it to a databindable control for example.

Another query with a specific output value (Title (Author) : Price):

var list_books_title = 	from c in
			select c.title + " (" + + ") Price:" + 
			c.price.ToString() + "$";
listBox1.DataSource = list_books_title.ToList();

Modify XML Data

To modify an XML attribute with Linq, you just need to query any object, modify its properties and finally save the database.

//query the book identified by bk001
var book001 = from c in
	      select c;

//remember that book001 is an IEnumarable, so you must check existence of first element
book001.First().price = 1234;

//save database (here a new file to see differences between files)

Removing and Creating Elements

Remove a Book

To delete an XML element, just query the object and call the Remove method of the database instance.

//select the book  bk001
var book001 = from c in
	      select c;

//remove it
foreach (var b in book001);

Create a New Book

To create a new XML element, just create a local object and add this item to the database instance.

//create the book instance
urn.books.BookForm itemToAdd = new urn.books.BookForm(); = "Andrew Troelsen";
itemToAdd.title = "Pro C# 2008 and the .NET 3.5 Platform";

//add it to database;

Strong Typed and Intellisense

One of the main interests of Linq To XSD (compared to Linq To XML) is that objects are strongly typed and can then directly be used by intellisense.
So it provides a very good way to gain productivity and avoid runtime errors. (The build detects errors because objects are really typed).


Points of Interest

  • Easy to use
  • Lesser code needed, so fewer bugs...
  • Fast implementation
  • Better than Linq To XML
  • Fully integrated by intellisense
  • Code is readable

Remaining Points

  • Linq to XSD is "Preview alpha"
  • Are the queries fast enough on large XML files?



  • 4th November, 2008: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer ECT Industries
France France
I am software engineer and I work for the aviation.

I'm currently working on many different project and in many different languages
- Visual C++ 6
- C#
- C and assembly

Have lot of fun

Comments and Discussions

GeneralMy vote of 5 Pin
mavericken4-Apr-12 17:29
mavericken4-Apr-12 17:29 
QuestionA link to CodePlex ploject Pin
Dmitry Pavlov21-Dec-11 5:11
professionalDmitry Pavlov21-Dec-11 5:11 
GeneralFeedback Pin
Gregory Nozik11-Sep-11 2:11
Gregory Nozik11-Sep-11 2:11 
GeneralUnable to open your download code Pin
jimdao31-Mar-10 9:54
jimdao31-Mar-10 9:54 
GeneralThanks! Pin
AgeKay29-Apr-09 5:11
AgeKay29-Apr-09 5:11 
Generalvs dynamics Pin
kosat7-Nov-08 0:35
kosat7-Nov-08 0:35 
QuestionMisunderstanding? Pin
Dmitri Nеstеruk6-Nov-08 21:54
Dmitri Nеstеruk6-Nov-08 21:54 
AnswerRe: Misunderstanding? Pin
Le Sourcier6-Nov-08 22:53
Le Sourcier6-Nov-08 22:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.