desdemona

reader

Nov 17th, 2012
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using System.Windows.Media.Imaging;
  13. using System.Xml;
  14. using System.Xml.Linq;
  15. using System.IO;
  16. using System.Text;
  17.  
  18.  
  19. namespace Promocje
  20. {
  21.     public partial class MainPage : UserControl
  22.     {
  23.  
  24.  
  25.         public class Produkt
  26.         {
  27.             public string nazwa { get; set; }
  28.             public string cena { get; set; }
  29.             public string obraz { get; set; }
  30.         }
  31.  
  32.         List<Produkt> lista = new List<Produkt>();
  33.  
  34.         int pr = 0;
  35.         public MainPage()
  36.         {
  37.             InitializeComponent();
  38.             LoadXMLFile("plik.xml");
  39.         }
  40.  
  41.         private void LoadXMLFile(string a)
  42.         {
  43.             WebClient xmlClient = new WebClient();
  44.             xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
  45.             xmlClient.DownloadStringAsync(new Uri(a, UriKind.RelativeOrAbsolute));
  46.         }
  47.  
  48.         void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
  49.         {
  50.             if (e.Error == null)
  51.             {
  52.                 string xmlData = e.Result;
  53.                 XmlReader reader = XmlReader.Create(new StringReader(e.Result));
  54.                 while (reader.Read())
  55.                 {
  56.                     if (reader.NodeType == XmlNodeType.Element && reader.Name == "produkt")
  57.                     {
  58.                         Produkt bogdan = new Produkt();
  59.                         reader.ReadToFollowing("nazwa");
  60.                         bogdan.nazwa = reader.ReadElementContentAsString();
  61.  
  62.                         reader.ReadToFollowing("cena");
  63.                         bogdan.cena = reader.ReadElementContentAsString();
  64.  
  65.                         reader.ReadToFollowing("obraz");
  66.                         bogdan.obraz = reader.ReadElementContentAsString();
  67.  
  68.                         lista.Add(bogdan);
  69.                     }
  70.  
  71.                     if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "promocje")
  72.                     {
  73.                         break;
  74.                     }
  75.                 }
  76.                 reader.Close();
  77.                 nazwaProduktu.Content = lista.First().nazwa;
  78.                 cena.Content = lista.First().cena;
  79.                 image1.Source = new BitmapImage(new Uri(lista.First().obraz, UriKind.Relative));
  80.                 pr = 0;
  81.                
  82.             }
  83.         }
  84.  
  85.  
  86.         private void Button1_Click(object sender, RoutedEventArgs e)
  87.         {
  88.             if (pr != 0)
  89.             {
  90.                 pr = (pr - 1) % lista.Count();
  91.                 image1.Source = new BitmapImage(new Uri(lista[pr].obraz, UriKind.Relative));
  92.                 cena.Content = lista[pr].cena;
  93.                 nazwaProduktu.Content = lista[pr].nazwa;
  94.             }
  95.             StartAnimation(sender, e);
  96.         }
  97.  
  98.  
  99.         private void Button2_Click(object sender, RoutedEventArgs e)
  100.         {
  101.             pr = (pr + 1) % lista.Count();
  102.             image1.Source = new BitmapImage(new Uri(lista[pr].obraz, UriKind.Relative));
  103.             cena.Content = lista[pr].cena;
  104.             nazwaProduktu.Content = lista[pr].nazwa;
  105.             StartAnimation(sender, e);
  106.         }
  107.  
  108.         private void StartAnimation(object sender, RoutedEventArgs args)
  109.         {
  110.             myStoryboard.Begin();
  111.         }
  112.  
  113.     }
  114.  
  115.  
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment