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

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.SerialSample1
  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.     //SerialSample1form1.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.       //new TextWriter and XmlSerializer
  104.       TextWriter tr=new StreamWriter("..\..\..\serialprod.xml");
  105.       XmlSerializer sr=new XmlSerializer(typeof(Products));
  106.       //serialize object
  107.       sr.Serialize(tr,pd);
  108.       tr.Close();
  109. MessageBox.Show("Serialized!");
  110.       
  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("..\..\..\serialprod.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(Namespace="", IsNullable=false)]
  130.     public class Products {
  131.       
  132.       [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  133.       public int ProductID;
  134.       
  135.       [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  136.       public string ProductName;
  137.       
  138.       [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  139.       public int SupplierID;
  140.       
  141.       [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  142.       public int CategoryID;
  143.       
  144.       [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  145.       public string QuantityPerUnit;
  146.       
  147.       [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  148.       public System.Decimal UnitPrice;
  149.       
  150.       [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  151.       public short UnitsInStock;
  152.       
  153.       [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  154.       public short UnitsOnOrder;
  155.       
  156.       [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  157.       public short ReorderLevel;
  158.       
  159.       [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  160.       public bool Discontinued;
  161.       
  162.     }
  163. }
  164. }