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.SerialSample2
  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. #region Windows Form Designer generated code
  49. /// <summary>
  50. /// Required method for Designer support - do not modify
  51. /// the contents of this method with the code editor.
  52. /// </summary>
  53.     private void InitializeComponent()
  54.     {
  55.       this.listBox1 = new System.Windows.Forms.ListBox();
  56.       this.button1 = new System.Windows.Forms.Button();
  57.       this.button2 = new System.Windows.Forms.Button();
  58.       this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  59.         | System.Windows.Forms.AnchorStyles.Right);
  60.       this.listBox1.Size = new System.Drawing.Size(336, 238);
  61.       this.listBox1.TabIndex = 0;
  62.       this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
  63.       this.button1.Location = new System.Drawing.Point(64, 264);
  64.       this.button1.TabIndex = 1;
  65.       this.button1.Text = "Serialize";
  66.       this.button1.Click += new System.EventHandler(this.button1_Click);
  67.       this.button2.Location = new System.Drawing.Point(152, 264);
  68.       this.button2.TabIndex = 2;
  69.       this.button2.Text = "Deserialize";
  70.       this.button2.Click += new System.EventHandler(this.button2_Click);
  71.       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  72.       this.ClientSize = new System.Drawing.Size(339, 320);
  73.       this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button2,
  74.                                                                   this.button1,
  75.                                                                   this.listBox1});
  76.       this.Text = "Form1";
  77.     }
  78. #endregion
  79. /// <summary>
  80. ///  The main entry point for the application.
  81. /// </summary>
  82. [STAThread]
  83. static void Main() 
  84. {
  85. Application.Run(new Form1());
  86. }
  87.     //SerialSample2form1.cs
  88.     private void button1_Click(object sender, System.EventArgs e)
  89.     {
  90.       //new products object
  91.       Products pd=new Products();
  92.       //set some properties
  93.       pd.ProductID=200;
  94.       pd.CategoryID=100;
  95.       pd.Discontinued=false;
  96.       pd.ProductName="Serialize Objects";
  97.       pd.QuantityPerUnit="6";
  98.       pd.ReorderLevel=1;
  99.       pd.SupplierID=1;
  100.       pd.UnitPrice=1000;
  101.       pd.UnitsInStock=10;
  102.       pd.UnitsOnOrder=0;
  103.       pd.Discount=2;
  104.       //new TextWriter and XmlSerializer
  105.       TextWriter tr=new StreamWriter("..\..\..\serialprod1.xml");
  106.       XmlSerializer sr=new XmlSerializer(typeof(Products));
  107.       //serialize object
  108.       sr.Serialize(tr,pd);
  109.       tr.Close();
  110.       MessageBox.Show("Serialized!");
  111.     }
  112.     private void button2_Click(object sender, System.EventArgs e)
  113.     {
  114.       //create a reference to producst type
  115.       Products newPd;
  116.       //new filestream to open serialized object
  117.       FileStream f=new FileStream("..\..\..\serialprod1.xml",FileMode.Open);
  118.       //new serializer
  119.       XmlSerializer newSr=new XmlSerializer(typeof(Products));
  120.       //deserialize the object
  121.       newPd=(Products)newSr.Deserialize(f);
  122.       //load it in the list box.
  123.       listBox1.Items.Add(newPd.ProductName);
  124.       f.Close();
  125.     }
  126.     
  127.     //class that will be serialized.
  128.     //attributes determine how object is serialized
  129.     [System.Xml.Serialization.XmlRootAttribute()]
  130.       public class Products {
  131.         private int prodId;
  132.         private string prodName;
  133.         private int suppId;
  134.         private int catId;
  135.         private string qtyPerUnit;
  136.         private Decimal unitPrice;
  137.         private short unitsInStock;
  138.         private short unitsOnOrder;
  139.         private short reorderLvl;
  140.         private bool discont;
  141.         private int disc;
  142.         //added the Discount attribute
  143.         [XmlAttributeAttribute(AttributeName="Discount")]
  144.         public int Discount {
  145.           get {return disc;}
  146.           set {disc=value;}
  147.         }
  148.    
  149.         [XmlElementAttribute()]
  150.         public int  ProductID {
  151.           get {return prodId;}
  152.           set {prodId=value;}
  153.         }
  154.         [XmlElementAttribute()]
  155.         public string ProductName {
  156.           get {return prodName;}
  157.           set {prodName=value;}
  158.         }
  159.         [XmlElementAttribute()]
  160.         public int SupplierID {
  161.           get {return suppId;}
  162.           set {suppId=value;}
  163.         }
  164.   
  165.         [XmlElementAttribute()]
  166.         public int CategoryID {
  167.           get {return catId;}
  168.           set {catId=value;}
  169.         }
  170.    
  171.         [XmlElementAttribute()]
  172.         public string QuantityPerUnit {
  173.           get {return qtyPerUnit;}
  174.           set {qtyPerUnit=value;}
  175.         }
  176.    
  177.         [XmlElementAttribute()]
  178.         public Decimal UnitPrice {
  179.           get {return unitPrice;}
  180.           set {unitPrice=value;}
  181.         }
  182.         [XmlElementAttribute()]
  183.         public short UnitsInStock {
  184.           get {return unitsInStock;}
  185.           set {unitsInStock=value;}
  186.         }
  187.    
  188.         [XmlElementAttribute()]
  189.         public short UnitsOnOrder {
  190.           get {return unitsOnOrder;}
  191.           set {unitsOnOrder=value;}
  192.         }
  193.    
  194.         [XmlElementAttribute()]
  195.         public short ReorderLevel {
  196.           get {return reorderLvl;}
  197.           set {reorderLvl=value;}
  198.         }
  199.   
  200.         [XmlElementAttribute()]
  201.         public bool Discontinued {
  202.           get {return discont;}
  203.           set {discont=value;}
  204.         }
  205.    
  206.       }
  207. }
  208. }