Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Xml;
- using System.Windows.Controls;
- using System.Collections.Generic;
- using System.Windows.Browser;
- using System.Net;
- namespace ClipBoard3
- {
- public partial class ClipReader : UserControl
- {
- private string titel = null;
- private string codeSnippet = null;
- private string test;
- //OrderedDictionary not implemented in Silverlight. Also, need indexes to find key/value pair. Normal Dictionary does not implement indexes.
- private List<KeyValuePair<string,string>> codeList;
- public ClipReader()
- {
- // Required to initialize variables
- InitializeComponent();
- codeList = new List<KeyValuePair<string, string>>();
- //Put all the codesnippets into the List<> instance.
- readXML();
- //Choose a snippet at random and display
- displaySnippet();
- }
- private void readXML() {
- Uri url = new Uri("codesnippets.xml", UriKind.Relative);
- WebClient client = new WebClient();
- client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
- client.DownloadStringAsync(url);
- }
- private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {
- test = e.Result;
- if(e.Error == null) {
- using(XmlReader reader = XmlReader.Create(e.Result)) {
- while(reader.Read()) {
- //Only detect start elements
- if(reader.IsStartElement()) {
- //Get element name and switch on it
- switch(reader.Name) {
- case "title":
- titel = reader.Value.Trim();
- break;
- case "code":
- codeSnippet = reader.Value.Trim();
- break;
- default:
- break;
- }
- //If both variables have been set, we have a complete set.
- if(codeSnippet != null) {
- //key and value can only be assigned in the constructor
- codeList.Add(new KeyValuePair<string, string>(titel, codeSnippet));
- //Set them back to null
- titel = null;
- codeSnippet = null;
- }
- }
- }
- }
- }
- }
- private void displaySnippet() {
- Random random = new Random();
- //The Next() function returns an Int32 between selected from [minValue, maxValue[
- int randomNumber = random.Next(0, codeList.Count);
- titelTxtBox.Text = HttpUtility.HtmlDecode(codeList[randomNumber].Key);
- CodeReadBox.Selection.Text = HttpUtility.HtmlDecode(codeList[randomNumber].Value);
- }
- private void makeOwnBtn_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- // TODO: Add event handler implementation here.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement