Parsing the results and binding the ListBox in Visual Basic .NET

Maker QR Code in Visual Basic .NET Parsing the results and binding the ListBox

Parsing the results and binding the ListBox
Encoding Quick Response Code In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create Denso QR Bar Code image in Visual Studio .NET applications.
www.OnBarcode.com
Quick Response Code Scanner In VB.NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
If you look in the output window from your last run, you ll see that the result format is an AtomPub document with an entry node for each of the results. In Silverlight, you can parse Atom a couple ways: you can use the built-in parsing of the SyndicationFeed class or you can use LINQ to XML to parse the results. LINQ to XML is a great technology and has many uses above and beyond AtomPub document parsing, so I m going to go that route. We ll end up with a little more code than the alternative approach, but I think it s worth it.
Code 128 Printer In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create Code128 image in .NET applications.
www.OnBarcode.com
DataMatrix Creation In VB.NET
Using Barcode generation for VS .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
TWEET CLASS
1D Encoder In Visual Basic .NET
Using Barcode drawer for VS .NET Control to generate, create Linear Barcode image in .NET applications.
www.OnBarcode.com
Drawing UPC A In VB.NET
Using Barcode creator for Visual Studio .NET Control to generate, create UPC-A Supplement 5 image in Visual Studio .NET applications.
www.OnBarcode.com
Before we do the actual parsing, we ll need to create a simple class to hold the content we re interested in. In Visual Studio, right-click the Silverlight project and choose Add > Class. Name the class Tweet.cs and fill it out so it looks like this:
Encode 2D In Visual Basic .NET
Using Barcode encoder for VS .NET Control to generate, create Matrix image in .NET framework applications.
www.OnBarcode.com
Make Code11 In VB.NET
Using Barcode creation for .NET Control to generate, create USD8 image in Visual Studio .NET applications.
www.OnBarcode.com
public class Tweet { public string Message { get; set; } public Uri Image { get; set; } }
Paint QR-Code In Objective-C
Using Barcode maker for iPhone Control to generate, create QR Code image in iPhone applications.
www.OnBarcode.com
QR Code 2d Barcode Recognizer In VB.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Download from Wow! eBook <www.wowebook.com>
PDF 417 Creator In .NET
Using Barcode creator for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
www.OnBarcode.com
Generate EAN13 In VS .NET
Using Barcode printer for ASP.NET Control to generate, create EAN / UCC - 13 image in ASP.NET applications.
www.OnBarcode.com
Building your first Silverlight web application
Code 3/9 Drawer In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create Code 3 of 9 image in ASP.NET applications.
www.OnBarcode.com
EAN / UCC - 13 Generator In None
Using Barcode generation for Font Control to generate, create GS1 - 13 image in Font applications.
www.OnBarcode.com
Save that class and move back to MainPage.xaml.cs. Somewhere inside the MainPage class, add the following collection variable. Above the GetTweets_Click method would be a perfect location:
Draw Data Matrix ECC200 In None
Using Barcode generator for Font Control to generate, create Data Matrix 2d barcode image in Font applications.
www.OnBarcode.com
Quick Response Code Drawer In .NET Framework
Using Barcode creator for .NET Control to generate, create QR Code JIS X 0510 image in VS .NET applications.
www.OnBarcode.com
private ObservableCollection<Tweet> _tweets = new ObservableCollection<Tweet>();
PDF417 Maker In Java
Using Barcode maker for Eclipse BIRT Control to generate, create PDF417 image in BIRT applications.
www.OnBarcode.com
Creating Barcode In VS .NET
Using Barcode encoder for .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Be sure to right-click the ObservableCollection type name and choose Resolve to add the appropriate using statement to your code. This collection will be the location where we place all of the parsed tweets. It s also what we ll bind the ListBox to. We ll use the ObservableCollection class in chapter 11 when we cover binding.
PDF-417 2d Barcode Creator In VS .NET
Using Barcode generator for Reporting Service Control to generate, create PDF417 image in Reporting Service applications.
www.OnBarcode.com
Generate QR Code ISO/IEC18004 In None
Using Barcode creator for Software Control to generate, create QR Code ISO/IEC18004 image in Software applications.
www.OnBarcode.com
PARSING WITH LINQ TO XML
LINQ is something you may have used on other .NET projects. If so, you ll feel right at home because it s supported in Silverlight as well. If not, it s pretty easy to pick up. Think of it almost like SQL but in code and working on objects and written backwards, with no database in sight. Okay, it s not exactly like SQL, but it s a great query language that lets you perform iterations and filters in a single line of code. In any case, you won t need to be a LINQ expert for this example. Right-click the project and choose Add Reference; add a reference to System. Xml.Linq. Figure 1.5 shows the dialog with the correct reference selected. Once the reference is added, replace the Debug.WriteLine statement and the event handler declaration in the code-behind with the code from listing 1.1. This code performs the actual parsing of the XML document returned by Twitter search and loads the tweets collection with the processed results.
The Add Reference dialog with System.Xml.Linq selected for LINQ to XML functionality
Download from Wow! eBook <www.wowebook.com>
Introducing Silverlight
Listing 1.1 Processing the Twitter search results using LINQ to XML
client.DownloadStringCompleted += (s, ea) => { XDocument doc = XDocument.Parse(ea.Result); XNamespace ns = "http://www.w3.org/2005/Atom";
B C D E
Atom namespace
var items = from item in doc.Descendants(ns + "entry") select new Tweet() { Message = item.Element(ns + "title").Value,
Image = new Uri(( from XElement xe in item.Descendants(ns + "link") where xe.Attribute("type").Value == "image/png" select xe.Attribute("href").Value ).First<string>()), }; foreach (Tweet t in items) { _tweets.Add(t); } };
Be sure to right-click and resolve the XDocument class in order to add the correct using statement to the top of your code. The code does some interesting processing. It first loads the results into an XDocument B so that it may be processed using LINQ statements. It then goes through the document selecting each entry element C and creating a new Tweet object from each D. The Tweet object itself is filled out by first grabbing the title element s value and assigning that to the Message and then doing another LINQ query to find the link element that has a type of image/png and assigning that to the Image property E. Finally, the code loops through each of the results and adds them to the tweets collection F. The namespace declaration at the top is necessary because the Atom namespace is the default xmlns in the document. When parsing XML, you need to have the default namespace declared or the results will be empty. With the parsing out of the way, the next step is to bind the ListBox to the _tweets collection so that it has a place to pull the data from.
Copyright © OnBarcode.com . All rights reserved.