Advertisement
Guest User

Untitled

a guest
Jun 14th, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1. using System;
  2. using System.Xml;
  3. using System.Windows.Controls;
  4. using System.Collections.Generic;
  5. using System.Windows.Browser;
  6. using System.Net;
  7.  
  8. namespace ClipBoard3
  9. {
  10.     public partial class ClipReader : UserControl
  11.     {
  12.         private string titel = null;
  13.         private string codeSnippet = null;
  14.         private string test;
  15.         //OrderedDictionary not implemented in Silverlight. Also, need indexes to find key/value pair. Normal Dictionary does not implement indexes.
  16.         private List<KeyValuePair<string,string>> codeList;
  17.  
  18.         public ClipReader()
  19.         {
  20.             // Required to initialize variables
  21.             InitializeComponent();
  22.  
  23.  
  24.             codeList = new List<KeyValuePair<string, string>>();
  25.  
  26.             //Put all the codesnippets into the List<> instance.
  27.             readXML();
  28.            
  29.             //Choose a snippet at random and display
  30.             displaySnippet();
  31.         }
  32.  
  33.         private void readXML() {
  34.             Uri url = new Uri("codesnippets.xml", UriKind.Relative);
  35.             WebClient client = new WebClient();
  36.             client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
  37.             client.DownloadStringAsync(url);
  38.         }
  39.  
  40.         private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {
  41.             test = e.Result;
  42.             if(e.Error == null) {
  43.                 using(XmlReader reader = XmlReader.Create(e.Result)) {
  44.                     while(reader.Read()) {
  45.                         //Only detect start elements
  46.                         if(reader.IsStartElement()) {
  47.  
  48.                             //Get element name and switch on it
  49.                             switch(reader.Name) {
  50.                                 case "title":
  51.                                     titel = reader.Value.Trim();
  52.                                     break;
  53.                                 case "code":
  54.                                     codeSnippet = reader.Value.Trim();
  55.                                     break;
  56.                                 default:
  57.                                     break;
  58.                             }
  59.  
  60.                             //If both variables have been set, we have a complete set.
  61.                             if(codeSnippet != null) {
  62.                                 //key and value can only be assigned in the constructor
  63.                                 codeList.Add(new KeyValuePair<string, string>(titel, codeSnippet));
  64.  
  65.                                 //Set them back to null
  66.                                 titel = null;
  67.                                 codeSnippet = null;
  68.                             }
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.  
  75.         private void displaySnippet() {
  76.             Random random = new Random();
  77.             //The Next() function returns an Int32 between selected from [minValue, maxValue[
  78.             int randomNumber = random.Next(0, codeList.Count);
  79.  
  80.  
  81.  
  82.             titelTxtBox.Text = HttpUtility.HtmlDecode(codeList[randomNumber].Key);
  83.             CodeReadBox.Selection.Text = HttpUtility.HtmlDecode(codeList[randomNumber].Value);
  84.         }
  85.  
  86.         private void makeOwnBtn_Click(object sender, System.Windows.RoutedEventArgs e)
  87.         {
  88.             // TODO: Add event handler implementation here.
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement