Click here to Skip to main content
6,822,123 members and growing! (18,643 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Data Binding     Advanced License: The Code Project Open License (CPOL)

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

By Balamurali Balaji

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.
C#3.0.NET3.5, XAML, WPF, LINQ, VS2008, Dev
Revision:4 (See All)
Posted:3 Jul 2009
Views:2,956
Bookmarked:8 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
0 votes for this article

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Balamurali Balaji


Member
Balamurali Balaji is a Trainer, Mentor, Developer providing training and solutions on .NET and other microsoft tools. One of his goals is to actively promote computer education through microsoft technologies and services in and around the state where he lives in India. Has around 15 yrs of experience in the IT industry. May not be seen readily in any IT events. Has conduted Training Programs and Seminars in hundreds of Engineering colleges and univeristies in India.

You may find his postings, articles, code etc, at http://www.dotnetspider.com. Most of his job related activities are found in his home page http://h1.ripway.com/bbmurali_2000. Has strong belief in Gandhian ideals and a true follower of him. Has worked on a private mission to build stronger relationship between India and the US.

Currently heading a self-styled organization and the website is BB Systems
Occupation: Founder
Company: BB Systems
Location: India India

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 3 Jul 2009
Editor: Smitha Vijayan
Copyright 2009 by Balamurali Balaji
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project