Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
RSS is a very popular format. In this movie, I want to show you how to bind your application to an RSS feed. I'm inside Internet Explorer, and I have my blog opened. It's blog.wpfwonderland.com. If I go to the end of my URL, and I type in the word "feed" and press Enter, I get the RSS feed view of the data, but it's still not the raw XML data that's coming from my server. What I should do to get the raw feed is right-click and then choose View Source.
I've already done that. I've put the file inside our project, inside Visual Studio. So let's switch to Visual Studio. Here is the feed file I'm talking about, BlogExampleFeed.xml. I'll double-click on it to load it into Visual Studio. Then I'm going to make my font a little bit smaller by using the mouse wheel and the Ctrl key. If I would like to bind part of my UI to this data, I need to figure out how to get down to this XML element. You can see that the description is a child of channel, and channel is a child of RSS.
There is a mechanism called XPath built into XML that lets me drill into this data using these tags. So that's what I'm going to use. I'm also going to use something called an XMLDataProvider, which comes from WPF. Let me switch to my main UI for one minute by double-clicking on MainWindow.xaml. Then click on the Your Blog tab. Then I'll click on this text block that's right here in the UI. Let's see what's happening here on line 82. The text for this TextBlock is going to come from a data source.
In WPF, the binding keyword is how we tell the WPF engine to get the data from this title element and put it in here on the Text property of the TextBlock. The curly braces are also part of the syntax for WPF and Silverlight. Notice that this TextBlock is bound to the description. Notice some dot dot stuff going on here, because I'm going to start in my data by looking at the individual items. These are the individual blog posts here. So I'm going to start my XPath by starting here. Then I'm going to go up a level to get to the description.
I need to write some code to set up this data binding. So I'm going to switch over to my MainWindow.xaml.cs file. Then I'm going to scroll down until I find this section down here where it says private void Window_Loaded. First, I need to instantiate the XMLDataProvider. I'm doing it outside of the Window_ Loaded event, so I can use it elsewhere in my code. Then after the move current to first bits of code, I'm going to tell that provider where to find my blog.
So here I've typed in my feed location. Next, I'm going to tell the provider where to start looking inside that document, using an XPath query. So we start with the root element, RSS. Then we go to channel, and then it's item. That's where I want to start looking. I can go up and down from that location in my bindings. Then lastly, I need to tell WPF to create a DataContext. So I'm going to go to the tab where all my RSS controls live, called tab4.
I'm going to set the DataContext equal to my provider. Now what this says is any WPF element that is inside this tab has access to the data coming from this XMLDataProvider. Then I'm going to press F5 to run the application. Save my changes. Then I'm going to click on the Your Blog tab, and I was successful. I'm pulling the data from my blog, loading it into this list box. When I click on an item in the list box, it's loading the actual blog article in over in this area.
I'm also pulling information about the blog name and the blog description and putting in these text blocks. I showed you how that was happening in the WPF.xaml earlier. There is one more thing I would like to make this application do. I'd like to go to the search feeds, and when you type in a new search, I'd like to go get the RSS from the Bing site and pass that in as the source for this page. It's relatively easy to do. I just need to go up to this area where I'm downloading the string from Bing.
I'm going to say provider.Source. I'm going to change from my blog source to the new source. I think that code should do it. Debug > Start Debugging, or F5. I will come over here and type in my new search "Databinding", click on the Get RSS button, and then I'm going to on the Your Blog tab, and see if I've pulled the data from Bing. So these are the top ten sites from the RSS feed that have the word "databinding" in them.
There are plenty of details I'm leaving out about databinding in this overview. You'll find more material about binding elsewhere in this course. At this point, our application is nearly complete. We just need to determine how to deploy it to the end user's computer.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98482 Viewers
61 Video lessons · 85764 Viewers
71 Video lessons · 69669 Viewers
56 Video lessons · 101993 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.