Click here to Skip to main content
15,614,421 members
Articles / Desktop Programming / WPF
Posted 3 Jul 2009


7 bookmarked

How to Perform WPF Data Binding Using LINQ to XML – Part 2

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Jul 2009CPOL2 min read
This article is Part 2 of the previous article and explains how to perform data binding to WPF controls using LINQ to data stored in an XML file.


This part of the article is a sequel to the previous article on how to develop a WPF application that can bind to XML data stored inline using LINQ to XML classes. In this part, I have discussed how to bind to an XML data stored in a file system.

It requires trivial changes in the XAML and the code-behind to bring in the same functionality here as we noted in the previous sample.

Change 1

Remember that we kept the XML chunk of data of our previous sample in the CDATA section, inline with the XAML code. Using the MethodName="Parse" specification in the Object DataProvider, this XML data is parsed and stored as an XElement instance.

Now, let us take a look at the resources section of the new XAML markup.

<ObjectDataProvider x:Key="LoadedBooks" 
  ObjectType="{x:Type xlinq:XElement}"  
  MethodName="Load" >

In the above markup, using the specification MethodName="Load" and by specifying the location (path) of the XML file, the XElement instance is created.

Change 2

The XML data is stored in the mydata.xml file. This XML data itself is different from what we have used inline in the previous sample. It has a list of books with an attribute “id” and an element “title”.

<books xmlns="">
  <book id="1">
    <title>Welcome, Home!</title>
  <book id="2">
    <title>Star Letters</title>
  <book id="3">
    <title>Pink and Blue</title>
  <book id="4">
    <title>Pirates of the Indian Ocean</title>

Change 3

To reflect these changes in the data binding aspect of the WPF controls, there is a slighter change in the markup as well.

Instead of Text="{Binding Path=Value}", now we need to code as Text="{Binding Path=Element[title].Value}".

Change 4

Similarly, while adding a new book in the existing list, do the following changes in the code for the handler “OnAddBook”:

XElement newBook = new XElement(mybooks + "book",
         new XAttribute("id", tbAddID.Text));
newBook.Add(new XElement("title", tbAddValue.Text));
bookList.LastNode.AddAfterSelf("  ", newBook, "\r\n");

Upon doing the above mentioned changes, run the sample code (Window2.xaml) and see the output as below:

Image 1


In this part of the article, we have seen the changes that need to be done in the previous part so as to bind to an external XML file from a WPF application using XLinq technology. The ObjectDataProvider is the core class that facilitates this XLinq functionality for binding both inline and external XML data. In the sample code application, Window1.Xaml presents the inline XML data binding, and Window2.Xaml uses an external file. Try it and enjoy the difference.


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

Written By
Founder BB Systems CIT-GPNP
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

-- There are no messages in this forum --