<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Silverlight &#124; WPF &#124; Microsoft.Net &#187; JSON</title>
	<atom:link href="http://joel.neubeck.net/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://joel.neubeck.net</link>
	<description>Simplifing structure without changing results</description>
	<lastBuildDate>Fri, 01 Apr 2011 21:34:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Silverlight how to: RSS feed stored as JSON in Isolated Storage</title>
		<link>http://joel.neubeck.net/2008/03/silverlight-how-to-rss-feed-stored-as-json-in-isolated-storage/</link>
		<comments>http://joel.neubeck.net/2008/03/silverlight-how-to-rss-feed-stored-as-json-in-isolated-storage/#comments</comments>
		<pubDate>Fri, 28 Mar 2008 22:07:51 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[DataContractJsonSerializer]]></category>
		<category><![CDATA[DispatcherTimer]]></category>
		<category><![CDATA[IsolatedStorage]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://joel.neubeck.net/2008/03/silverlight-how-to-rss-feed-stored-as-json-in-isolated-storage/</guid>
		<description><![CDATA[This week I was reading two of my favorite Microsoft Evangelist blogs; Kirk Allen Evans&#8217; and Tim Heuer and was inspired to build upon two of their posts. Kirk wrote a great entry on Creating a JSON Service with WebGet and WCF 3.5 and Tim on Calling web services with Silverlight 2. I thought it [...]]]></description>
			<content:encoded><![CDATA[<p>This week I was reading two of my favorite Microsoft Evangelist blogs; <a href="http://blogs.msdn.com/kaevans">Kirk Allen Evans&#8217;</a> and <a href="http://timheuer.com">Tim Heuer</a> and was inspired to build upon two of their posts.  Kirk wrote a great entry on <a href="http://tinyurl.com/3cbld3">Creating a JSON Service with WebGet and WCF 3.5</a> and Tim on<br />
<a href="http://tinyurl.com/3bv5ch">Calling web services with Silverlight 2</a>.  I thought it would be interesting to take the concepts covered in both of these posts and put them together into my own how to.  In my sample I read an RSS feed into a JSON string, stores it in Isolated storage, and displays it in a ListBox.  Once displayed, I will check every 30 seconds to see if the RSS feed has changed.  Out-of-the-box my solution is not practical, but illustrates a flexible technique for caching a serialized collection of data in the event the service is unavailable. </p>
<p>In the first part of my sample I check to see if I have a cache of RSS in Isolated Storage.  If so, I use the &#8220;DataContractJsonSerializer&#8221; class to de-serialize the JSON array.  Once de-serialized, I can bind it to my ListBox control in &#8220;Page.xaml&#8221;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="c-sharp" style="font-family:monospace;">void Page_Loaded(object sender, RoutedEventArgs e)
{
    RssService.RssItem[] items;
    if (_isf.FileExists(&quot;blog.json&quot;))
    {
        //Read from IsolatedStorage the last set of blog entires
        using (IsolatedStorageFileStream isfs = 
         new IsolatedStorageFileStream(&quot;blog.json&quot;, System.IO.FileMode.Open, _isf))
        {
            //Deserialize the JSON array that was stored in Isolated storage.
            DataContractJsonSerializer djson = 
             new DataContractJsonSerializer(typeof(RssService.RssItem[]));
&nbsp;
            items = djson.ReadObject(isfs) as RssService.RssItem[];
&nbsp;
            isfs.Position = 0;
            using (StreamReader sr = new StreamReader(isfs))
            {
                this.txtBlock.Text = sr.ReadToEnd();
            }
            isfs.Close();
        }
        listBox.ItemsSource = items;
    }
&nbsp;
    . . . . . . 
}</pre></td></tr></table></div>

<p>After we update our presentation of RSS, I will insert a animating UserControl to be use when we go to retrieve additional RSS items.  This retrieval from our WCF 3.5 service will be executed every 10 seconds when our &#8220;DispatcherTimer&#8221; fires.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="c-sharp" style="font-family:monospace;">    //insert a UserControl that animates a spinner when we are retrieving 
    //an update of RSS data
    _indicator.HorizontalAlignment = HorizontalAlignment.Center;
    _indicator.VerticalAlignment = VerticalAlignment.Center;
    this.LayoutRoot.Children.Add(_indicator);
    _indicator.Visibility = Visibility.Collapsed;
&nbsp;
    //we will use a timer here to simulate a 5 second delay in grabbing 
    //the next rss update
    _dt.Interval = new TimeSpan(0, 0, 10);
    _dt.Tick += new EventHandler(_dt_Tick);
    _dt.Start();</pre></td></tr></table></div>

<p>Upon successful retrieval from our WCF service, we will update our isolated storage by serializing our array of RssItems[] into JSON, and writing that text to a file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="c-sharp" style="font-family:monospace;">void wcf_GetFeedsCompleted(object sender, 
    IsolatedStorage.RssService.GetFeedsCompletedEventArgs e)
{
    try
    {
        RssService.RssItem[] items = e.Result as RssService.RssItem[];
        //if we were able to grab a new set of Rss items then update Isolated storage
        if (items.Length &gt; 0)
        {
            if (_isf.FileExists(&quot;blog.json&quot;))
            {
                _isf.DeleteFile(&quot;blog.json&quot;);
            }
            using (IsolatedStorageFileStream isfs = 
             new IsolatedStorageFileStream(&quot;blog.json&quot;, System.IO.FileMode.Create, _isf))
            {
                //Take the array of RssItem[] objects and convert it to a JSON array.
                DataContractJsonSerializer djson = new DataContractJsonSerializer(items.GetType());
                MemoryStream ms = new MemoryStream();
                djson.WriteObject(ms, items);
&nbsp;
                this.txtBlock.Text = System.Text.Encoding.UTF8.GetString
                   (ms.GetBuffer(), 0, Convert.ToInt16(ms.Length));
&nbsp;
                isfs.Write(ms.GetBuffer(), 0, Convert.ToInt16(ms.Length));
                isfs.Close();
            }
            listBox.ItemsSource = items;
        }
        this.listBox.Opacity = 1;
        _indicator.Animation.Stop();
        _indicator.Visibility = Visibility.Collapsed;
    }
    catch (Exception ex)
    {
        this.txtBlock.Text = ex.Message;
    }
}</pre></td></tr></table></div>

<p>Thanks again to Tim and Kirk for the majority of my example.</p>
<p>Code: <a href="/wp-content/uploads/2008/03/IsolatedStorage.zip" onClick="javascript: pageTracker._trackPageview('/code/IsolatedStorage.zip');">IsolatedStorage.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2008/03/silverlight-how-to-rss-feed-stored-as-json-in-isolated-storage/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

