Home » Articles


Sponsored links

CodeDigest Navigation

Technology News
No News Feeds available at this time.

Community News
No News Feeds available at this time.
How to perform WPF Data Binding using LINQ to XML – Part 2

By Balamurali Balaji
Posted On Apr 27,2009
Article Rating:
Average Rating: 5
No of Ratings: 1
No of Comments: 5
Category: WPF
Print this article.

How to perform WPF Data Binding using LINQ to XML – Part 2


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" >


                <system:String>d:\bala\mydata.xml </system:String>




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


Change 2

The XML data is stored in 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 data binding aspect of the WPF controls, there is a sligher change in the markup as well.


Instead of                                      Text="{Binding Path=Value}"


Now, we need to write               Text="{Binding Path=Element[title].Value}"


Change 4

Similary, 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:




Download Source 


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.

Similar Articles
You can contribute to CodeDiget.Com:
Donate to
Article Feedback
Query on <Page

How do I use this code for a WPF <Page> so that I can open my web page and sue this XML binding technique?

You got the answer

Thanks for asking me again. I think you have misunderstood the concept of Two-way binding.

The objectDataProvider I am using in this application is capable of Two-way binding, that means that it can do both reading and writing of Xml data in a file. The sample provided in this article demonstrates the functionalities of adding a new book, editing and removing a book to this xml file.

However, if you make changes in the filesystem, i.e., you edit in the xml file and add a new book during application runtime, the changes would not reflect in the bound controls that display them. You need to refresh the binding mechanism once again to see the newly modified xml content. To do so, you may add a new button and on click of a button, you may enable refreshing of the objectdataprovider.


Paste the above code in any event handler so that the resource binding is refreshed to enable the changes been reflected in the application bound controls.

Hope this is clear.

re:Binding Answer
Hi Bala,

Thanks very much for this reply. Below is something I have tried. Can you please correct me if there is anything wrong with what I did or what i expected.

(1) I downloaded the source, and opened it in VS 2008. deleted Windows3 and leagues.xml as they are not inlcuded in the download. then I could build and run the solution.
(2) while this app was running, I modied mydata.xml by adding a book, like.
<book id="5">
(3) I expected the new book to be displayed on the UI.
Here come my questions.
Should I expect this?
Does "changes to the source are automatically propagated to the target" really suggest I should expect for this?
(4) the result I saw from the app is that the new book shows on the UI only if restart the app. Is this actually what One Time Binging means?

I think I misunderstood "changes to the source are automatically propagated to the target", and shouldn't expect UI to reflect the changes I made to mydata.xml at run time.

I'll appreciate if you can correct my understanding by putting more comments.

Thank you very much!
Binding Answer

The dynamic binding happens through notification mechanism implemented in the class file. It is already specified in the article. Please read the paras:
By default, data binding occurs only when the target UI element is initialized. This is called one-time binding. For most purposes, this is insufficient; with two-way binding, changes to the source are automatically propagated to the target, and changes to the target are automatically propagated to the source.

For one-way or two-way binding to occur, the source must implement a change notification mechanism, for example by implementing the INotifyPropertyChanged interface or by using a PropertyNameChanged pattern for each property supported.


IF you look at the code, you could see this implementation.

Thanks for asking.
Dynamic Data Binding
Nice article!

the section "Dynamic Data Binding in WPF and Dynamic Properties" of the first part of this article mentions "......
changes to the source are automatically propagated to the target, ......". That is what I'm most interested in and how I googled out your this article.

just wondering whether the sample app demonstrates that or not. if I missed something or misunderstood something of this article, please forgive me. I'm just so so so curious
about that.

What is an easy way to make the UI target to reflect the changes made to the source xml file at runtime? if the article had already answered the question, please forgive me again, can you please give me more hints? because while I play with the downloaded sample code, just cannot find out this feature.

or can you suggest or direct me to some other references if you know there is one.

thanks again. this is a good article even though I haven't found the anwser to my question yet.