Peter McGrattan’s Weblog

Silverlight, WCF, ASP.NET, AJAX, Graphics, RIA

Silverlight 2 Stock List Demo – Part 1

Posted by petermcg on April 17, 2008

Edited on 26/June/2008 : Code download updated to support Silverlight 2 Beta 2

Edited on 20/October/2008 : Code download updated to support Silverlight 2 RTW

Part One : From XML file to DataGrid via LINQ to XML – (Code download)

One of the great features new to Silverlight 2.0 Beta 1 is the support for LINQ to XML available in the System.Core and System.Xml.Linq assemblies. Part 1 of this post shows a simple Silverlight application that populates a DataGrid with sample financial stock information read from an XML file using LINQ to XML.

This is the layout of our initial Visual Studio 2008 solution structure :

Solution Structure

Our solution contains three projects, the Silverlight Application project StockListDemo.App and it’s hosting Web Site, and a Silverlight Class Library (StockLibrary). Notice in our Silverlight Application we have a folder called Data containing an XML file pre-populated as follows :

<?xml version="1.0" encoding="utf-8" ?>
  <stock symbol="AAHH" bid="256.02" ask="256.70" />
  <stock symbol="AAPM" bid="181.31" ask="182.03" />
  <stock symbol="AAZG" bid="286.51" ask="286.93" />
  <stock symbol="ABOO" bid="226.74" ask="227.69" />
  <stock symbol="ADJS" bid="109.35" ask="109.92" />
  <stock symbol="AELX" bid="91.52" ask="92.43" />

Each <stock /> element in the Stocks.xml file consists of a symbol name for the stock, a bid price and an ask price. Although it’s easy to see where the file resides at design time, it’s not immediately obvious how to access it at runtime. When in this situation it’s a good idea to check the ‘Build Action’, you can do this by clicking on the file and pressing F4 :

Stocks.xml Build Action

When you add a new XML file to a Silverlight Application in Visual Studio 2008 the Build Action is set to ‘Content’ by default which means the file and it’s folder structure will be packaged at the root of the Silverlight .xap deployment package. A .xap file is packaged when you build a Silverlight Application by the Chiron.exe SDK tool and is really just a .zip file. If we open it in a .zip file application after a build you can see the Data folder containing the .xml file :

Stocks.xml in Xap Extract

Now we know where the file is at runtime, the question is how do we get at it and read it. Here’s one solution, but the title of this post says LINQ to XML so here’s the code to read the 1000 stock elements from the XML file in the .xap and assign the results to a collection :

public List<Stock> GetStocks()
    List<Stock> stocks = new List<Stock>();

    XElement doc = XElement.Load("Data/Stocks.xml");

    stocks = (from el in doc.Elements()
              select GetStock(el)).ToList();

    return stocks;

private Stock GetStock(XElement el)
    Stock s = new Stock();
    s.Symbol = el.Attribute("symbol").Value;
    s.Bid = Convert.ToDouble(el.Attribute("bid").Value);
    s.Ask = Convert.ToDouble(el.Attribute("ask").Value);
    return s;

The two methods above are part of the XmlFileStockService class in the StockLibrary project, you can download the whole small example solution below. The GetStocks method is required by the IStockService interface which the XmlFileStockService class implements. Another class in the same assembly is the StockTicker class. StockTicker has a private field of type IStockService and calls GetStocks on this field to expose the collection in the StockList property. Page.xaml has an instance of StockTicker and assigns it’s StockList property to the ItemsSource property of a DataGrid called StocksGrid with AutoGenerateColumns="True" :

Stock List DataGrid

In a real world situation this data would come from a source such as a web service, now that Silverlight supports cross-domain calls, the data for this type of RIA application can really come from a wide variety of sources. This is a sample application but with the IStockService interface has still been designed to require a minimum of changes for swapping out the datasource from an XML file to a WCF service for example. Some of the areas I intend to take a look at in future posts in this series are : incorporating a web service as a datasource, getting the stock list to tick with modifications and additions and more.

About these ads

2 Responses to “Silverlight 2 Stock List Demo – Part 1”

  1. zozo said

    Thanks for good post.. but where is the simple code for the above example.. I would like to have that am doing something similar tried to work wiht your Webservice examplebut it is so complicated with so many classes?? can you post the simple example ?/

  2. Peter McGrattan said

    Hi Zozo, Thanks for your comment, I’ve now updated the code download link for this post to point to the Beta2 version of the simple part 1 demo.

Sorry, the comment form is closed at this time.


Get every new post delivered to your Inbox.

%d bloggers like this: