我在做一个学生信息管理系统,然后,在退出时准备把信息存入文件中,如何使用Serialize()和 DeSerialize()方法。main函数我已经有 了,那Serialize()和 DeSerialize()怎么初始化啊

解决方案 »

  1.   

    这个是xml文件的using System;
    using System.IO;
    using System.Xml.Serialization;// This is the class that will be serialized.
    public class OrderedItem
    {
       public string ItemName;
       public string Description;
       public decimal UnitPrice;
       public int Quantity;
       public decimal LineTotal;   // A custom method used to calculate price per item.
       public void Calculate()
       {
          LineTotal = UnitPrice * Quantity;
       }
    }public class Test{
       public static void Main(string[] args)
       {
          Test t = new Test();
          // Write a purchase order.
          t.SerializeObject("simple.xml");
       }   private void SerializeObject(string filename)
       {
          Console.WriteLine("Writing With Stream");      XmlSerializer serializer = 
          new XmlSerializer(typeof(OrderedItem));
          OrderedItem i = new OrderedItem();
          i.ItemName = "Widget";
          i.Description = "Regular Widget";
          i.Quantity = 10;
          i.UnitPrice = (decimal) 2.30;
          i.Calculate();      // Create a FileStream to write with.
          Stream writer = new FileStream(filename, FileMode.Create);
          // Serialize the object, and close the TextWriter
          serializer.Serialize(writer, i);
          writer.Close();
       }
    }
      

  2.   

    using System;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;// This is the class that will be deserialized.
    public class OrderedItem
    {
        public string ItemName;
        public string Description;
        public decimal UnitPrice;
        public int Quantity;
        public decimal LineTotal;    // A custom method used to calculate price per item.
        public void Calculate()
        {
            LineTotal = UnitPrice * Quantity;
        }
    }
    public class Test
    {
        public static void Main(string[] args)
        {
            Test t = new Test();
            // Read a purchase order.
            t.DeserializeObject("simple.xml");
        }    private void DeserializeObject(string filename)
        {
            Console.WriteLine("Reading with XmlReader");        // Create an instance of the XmlSerializer specifying type and namespace.
            XmlSerializer serializer = new
            XmlSerializer(typeof(OrderedItem));        // A FileStream is needed to read the XML document.
            FileStream fs = new FileStream(filename, FileMode.Open);
            XmlReader reader = XmlReader.Create(fs);        // Declare an object variable of the type to be deserialized.
            OrderedItem i;        // Use the Deserialize method to restore the object's state.
            i = (OrderedItem)serializer.Deserialize(reader);
            fs.Close();        // Write out the properties of the object.
            Console.Write(
            i.ItemName + "\t" +
            i.Description + "\t" +
            i.UnitPrice + "\t" +
            i.Quantity + "\t" +
            i.LineTotal);
        }
    }
      

  3.   

    楼上的都是CSDN 上的,真解了。然后,就是涉及的概念是:对象的序列化,与反序列化
      

  4.   

    要注意,你定义的类必须有属性Serializable