Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: C# | Size: 4.11 KB | Hits: 54 | Expires: Never
Copy text to clipboard
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Xml.Serialization;
  8.  
  9.  
  10. /// <summary>
  11. /// Summary description for Book
  12. /// </summary>
  13.  
  14. [XmlRoot("BookStore")]
  15. public class BookStore
  16. {
  17.     List<Book> booksList;
  18.  
  19.     public BookStore()
  20.     {
  21.         booksList = new List<Book>();
  22.     }
  23.  
  24.     public void LoadFromXML(string path)
  25.     // מטודה מקבלת קובץ קסמל לטעון ממנו את הנתונים
  26.     // לדוגמא: LoadFromXML(Server.MapPath("list.xml"));
  27.     {
  28.         XmlSerializer s = new XmlSerializer(typeof(BookStore));
  29.         TextReader r = new StreamReader(path);
  30.         booksList = ((BookStore)s.Deserialize(r)).BooksList;
  31.         r.Close();
  32.     }
  33.  
  34.     public void SaveToXML(string path)
  35.     // מטודה שומרת נתונים לקובץ קסמל
  36.     // לדוגמא: SaveToXML(Server.MapPath("list.xml"));
  37.     {
  38.         XmlSerializer s = new XmlSerializer(typeof(BookStore));
  39.         TextWriter w = new StreamWriter(path);
  40.         s.Serialize(w, this);
  41.         w.Close();
  42.     }
  43.  
  44.     public void AddBook(Book book)
  45.     {
  46.         booksList.Add(book);
  47.     }
  48.  
  49.     public void RemoveBook(int i)
  50.     {
  51.         booksList.RemoveAt(i);
  52.     }
  53.  
  54.     public void RemoveBook(string name)
  55.     {
  56.         for (int i = 0; i < booksList.Count; i++)
  57.             if (booksList[i].Name == name)
  58.             {
  59.                 booksList.RemoveAt(i);
  60.                 break;
  61.             }
  62.     }
  63.  
  64.     [XmlElement("Book")]
  65.     public List<Book> BooksList
  66.     {
  67.         get { return booksList; }
  68.         set { booksList = value; }
  69.     }
  70. }
  71.  
  72. public class Book
  73. {
  74.     [XmlAttribute("name")]
  75.     private string name;
  76.     [XmlAttribute("imageurl")]
  77.     private string imageurl;
  78.     [XmlAttribute("subject")]
  79.     private string subject;
  80.     [XmlAttribute("author")]
  81.     private string author;
  82.     [XmlAttribute("level")]
  83.     private int level;
  84.     [XmlAttribute("year")]
  85.     private int year;
  86.     [XmlAttribute("rating")]
  87.     private int rating;
  88.     [XmlAttribute("booksellers")]
  89.     private List<string> booksellers;
  90.     [XmlAttribute("bookprices")]
  91.     private List<double> bookprices;
  92.  
  93.     public Book()
  94.     {
  95.         booksellers = new List<string>();
  96.         bookprices = new List<double>();
  97.     }
  98.  
  99.     public Book(string name, string imageurl, string subject, string author, int level, int year, int rating)
  100.     {
  101.         this.name = name;
  102.         this.imageurl = imageurl;
  103.         this.subject = subject;
  104.         this.author = author;
  105.         this.level = level;
  106.         this.year = year;
  107.         this.rating = rating;
  108.  
  109.         booksellers = new List<string>();
  110.         bookprices = new List<double>();
  111.     }
  112.  
  113.     public void AddSeller(string seller, double price)
  114.     {
  115.         booksellers.Add(seller);
  116.         bookprices.Add(price);
  117.     }
  118.  
  119.     public void RemoveSeller(string seller)
  120.     {
  121.         int i = booksellers.IndexOf(seller);
  122.         booksellers.RemoveAt(i);
  123.         bookprices.RemoveAt(i);
  124.     }
  125.  
  126.     public string Name
  127.     {
  128.         get { return name; }
  129.         set { name = value; }
  130.     }
  131.  
  132.     public string Imageurl
  133.     {
  134.         get { return imageurl; }
  135.         set { imageurl = value; }
  136.     }
  137.  
  138.     public string Subject
  139.     {
  140.         get { return subject; }
  141.         set { subject = value; }
  142.     }
  143.  
  144.     public string Author
  145.     {
  146.         get { return author; }
  147.         set { author = value; }
  148.     }
  149.  
  150.     public int Level
  151.     {
  152.         get { return level; }
  153.         set { if (value > 0) level = value; }
  154.     }
  155.  
  156.     public int Year
  157.     {
  158.         get { return year; }
  159.         set { if ((value > 1900) && (value <= DateTime.Now.Year)) year = value; }
  160.     }
  161.  
  162.     public int Rating
  163.     {
  164.         get { return rating; }
  165.         set { if ((rating >= 1) && (rating <= 5)) rating = value; }
  166.     }
  167.  
  168.     public List<string> BookSellers
  169.     {
  170.         get { return booksellers; }
  171.         set { booksellers = value; }
  172.     }
  173.  
  174.     public List<double> BookPrices
  175.     {
  176.         get { return bookprices; }
  177.         set { bookprices = value; }
  178.     }
  179. }