Form1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:6k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8. using System.Xml;
  9. using System.Xml.Serialization;
  10. namespace Wrox.ProCSharp.Xml.SerialSample3
  11. {
  12. /// <summary>
  13. /// Summary description for Form1.
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.ListBox listBox1;
  18. private System.Windows.Forms.Button button1;
  19.     private System.Windows.Forms.Button button2;
  20. /// <summary>
  21. /// Required designer variable.
  22. /// </summary>
  23. private System.ComponentModel.Container components=null;
  24. public Form1()
  25. {
  26. //
  27. // Required for Windows Form Designer support
  28. //
  29. InitializeComponent();
  30. //
  31. // TODO: Add any constructor code after InitializeComponent call
  32. //
  33. }
  34. /// <summary>
  35. /// Clean up any resources being used.
  36. /// </summary>
  37.    protected override void Dispose( bool disposing )
  38.     {
  39.     if( disposing )
  40.     {
  41.     if (components != null) 
  42.     {
  43.     components.Dispose();
  44.     }
  45.     }
  46.     base.Dispose( disposing );
  47.     }
  48.    
  49. #region Windows Form Designer generated code
  50. /// <summary>
  51. /// Required method for Designer support - do not modify
  52. /// the contents of this method with the code editor.
  53. /// </summary>
  54.     private void InitializeComponent()
  55.     {
  56.       this.listBox1 = new System.Windows.Forms.ListBox();
  57.       this.button1 = new System.Windows.Forms.Button();
  58.       this.button2 = new System.Windows.Forms.Button();
  59.       this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  60.         | System.Windows.Forms.AnchorStyles.Right);
  61.       this.listBox1.Size = new System.Drawing.Size(336, 238);
  62.       this.listBox1.TabIndex = 0;
  63.       this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
  64.       this.button1.Location = new System.Drawing.Point(64, 264);
  65.       this.button1.TabIndex = 1;
  66.       this.button1.Text = "Serialize";
  67.       this.button1.Click += new System.EventHandler(this.button1_Click);
  68.       this.button2.Location = new System.Drawing.Point(152, 264);
  69.       this.button2.TabIndex = 2;
  70.       this.button2.Text = "Deserialize";
  71.       this.button2.Click += new System.EventHandler(this.button2_Click);
  72.       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  73.       this.ClientSize = new System.Drawing.Size(339, 320);
  74.       this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button2,
  75.                                                                   this.button1,
  76.                                                                   this.listBox1});
  77.       this.Text = "Form1";
  78.     }
  79. #endregion
  80. /// <summary>
  81. ///  The main entry point for the application.
  82. /// </summary>
  83. [STAThread]
  84. static void Main() 
  85. {
  86. Application.Run(new Form1());
  87. }
  88.     //SerialSample3form1.cs
  89.     private void button1_Click(object sender, System.EventArgs e)
  90.     {
  91.       //create new book and bookproducts objects
  92.       Product newProd=new Product();
  93.       BookProduct newBook=new BookProduct();
  94.       //set som eproperties
  95.       newProd.ProductID=100;
  96.       newProd.ProductName="Product Thing";
  97.       newProd.SupplierID=10;
  98.       
  99.       newBook.ProductID=101;
  100.       newBook.ProductName="How to Use Your New Product Thing";
  101.       newBook.SupplierID=10;
  102.       newBook.ISBN="123456789";
  103.       //add the items to an array
  104.       Product[] addProd={newProd,newBook};
  105.       //new inventory object using the addProd array
  106.       Inventory inv=new Inventory();
  107.       inv.InventoryItems=addProd;
  108.       //serialize the Inventory object
  109.       TextWriter tr=new StreamWriter("..\..\..\order.xml");
  110.       XmlSerializer sr=new XmlSerializer(typeof(Inventory));
  111.       
  112.       sr.Serialize(tr,inv);
  113.       tr.Close();
  114. MessageBox.Show("Serialized!");
  115.       
  116.     }
  117.     private void button2_Click(object sender, System.EventArgs e)
  118.     {
  119.       //create Inventory object
  120.       Inventory newInv;
  121.       
  122.       FileStream f=new FileStream("..\..\..\order.xml",FileMode.Open);
  123.       //deserialize the order.xml doc
  124.       XmlSerializer newSr=new XmlSerializer(typeof(Inventory));
  125.       newInv=(Inventory)newSr.Deserialize(f);
  126.       //load listbox with data from deserialized object
  127.       foreach(Product prod in newInv.InventoryItems)
  128.         listBox1.Items.Add(prod.ProductName);
  129.         
  130.       f.Close();
  131.     }
  132.     
  133.     
  134.     public class Inventory
  135.     {
  136.       private Product[] stuff;
  137.       //ctor
  138.       public Inventory() {}
  139.       //need have an attribute entry for each data type
  140.       [XmlArrayItem("Prod",typeof(Product)),
  141.       XmlArrayItem("Book",typeof(BookProduct))]
  142.      
  143.       public Product[] InventoryItems {
  144.         get {return stuff;}
  145.         set {stuff=value;}
  146.       }
  147.     
  148.     }
  149.     
  150.     //product class
  151.     public class Product {
  152.     
  153.       private int prodId;
  154.       private string prodName;
  155.       private  int suppId;
  156.      
  157.       public Product() {}
  158.        
  159.     
  160.       public int  ProductID {
  161.         get {return prodId;}
  162.         set {prodId=value;}
  163.       }
  164.       
  165.     
  166.       public string ProductName {
  167.         get {return prodName;}
  168.         set {prodName=value;}
  169.       }
  170.       
  171.     
  172.       public int SupplierID  {
  173.         get {return suppId;}
  174.         set {suppId=value;}
  175.       }
  176.      
  177.       
  178.     }
  179.     //Bookproduct class
  180.     public class BookProduct:Product
  181.     {
  182.       private string isbnNum;
  183.       
  184.       public BookProduct() {}
  185.       
  186.       public string ISBN  {
  187.         get {return isbnNum;}
  188.         set {isbnNum=value;}
  189.       }
  190.     
  191.     }
  192.     
  193.     
  194.     
  195. }
  196. }