Click here to Skip to main content
15,867,986 members
Articles / Desktop Programming / WPF

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 26K   574   7  
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.

XML
<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”.

XML
<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”:

C#
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:

Image 1

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