Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
3 Jul 2009 1  
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.

Introduction

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" >
    <ObjectDataProvider.MethodParameters>
        <system:String>d:\bala\mydata.xml</system:String>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

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>
  <book id="2">
    <title>Star Letters</title>
  </book>
  <book id="3">
    <title>Pink and Blue</title>
  </book>
  <book id="4">
    <title>Pirates of the Indian Ocean</title>
  </book>
</books>

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");
lbBooks.Items.Refresh();

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

Conclusion

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here