Example #1
0
        public static void Run()
        {
            TestSimpleObject obj = new TestSimpleObject();

            Console.WriteLine("Before serialization the object contains: ");
            obj.Print();

            Stream        stream    = File.Open("data.xml", FileMode.Create);
            SoapFormatter formatter = new SoapFormatter();

            formatter.Serialize(stream, obj);
            stream.Close();

            obj = null;

            stream    = File.Open("data.xml", FileMode.Open);
            formatter = new SoapFormatter();

            obj = (TestSimpleObject)formatter.Deserialize(stream);
            stream.Close();

            Console.WriteLine("");
            Console.WriteLine("After deserialization the object contains: ");
            obj.Print();
        }
Example #2
0
    public static void Main()
    {
        //Creates a new TestSimpleObject object.
        TestSimpleObject obj = new TestSimpleObject();

        Console.WriteLine("Before serialization the object contains: ");
        obj.Print();

        //Opens a file and serializes the object into it in binary format.
        Stream        stream    = File.Open("data.xml", FileMode.Create);
        SoapFormatter formatter = new SoapFormatter();

        //BinaryFormatter formatter = new BinaryFormatter();

        formatter.Serialize(stream, obj);
        stream.Close();

        //Empties obj.
        obj = null;

        //Opens file "data.xml" and deserializes the object from it.
        stream    = File.Open("data.xml", FileMode.Open);
        formatter = new SoapFormatter();

        //formatter = new BinaryFormatter();

        obj = (TestSimpleObject)formatter.Deserialize(stream);
        stream.Close();

        Console.WriteLine("");
        Console.WriteLine("After deserialization the object contains: ");
        obj.Print();
        Console.ReadLine();
    }
Example #3
0
    public static void Main()
    {
        // Create a new TestSimpleObject object.
        TestSimpleObject obj = new TestSimpleObject();

        Console.WriteLine("\n Before serialization the object contains: ");
        obj.Print();

        // Open a file and serialize the object into binary format.
        Stream          stream    = File.Open("DataFile.dat", FileMode.Create);
        BinaryFormatter formatter = new BinaryFormatter();

        try
        {
            formatter.Serialize(stream, obj);

            // Print the object again to see the effect of the
            //OnSerializedAttribute.
            Console.WriteLine("\n After serialization the object contains: ");
            obj.Print();

            // Set the original variable to null.
            obj = null;
            stream.Close();

            // Open the file "DataFile.dat" and deserialize the object from it.
            stream = File.Open("DataFile.dat", FileMode.Open);

            // Deserialize the object from the data file.
            obj = (TestSimpleObject)formatter.Deserialize(stream);

            Console.WriteLine("\n After deserialization the object contains: ");
            obj.Print();
            Console.ReadLine();
        }
        catch (SerializationException se)
        {
            Console.WriteLine("Failed to serialize. Reason: " + se.Message);
            throw;
        }
        catch (Exception exc)
        {
            Console.WriteLine("An exception occurred. Reason: " + exc.Message);
            throw;
        }
        finally
        {
            stream.Close();
            obj       = null;
            formatter = null;
        }
    }
Example #4
0
    public static void Main()
    {
        // 三种序列化对比:Binary模式不生成xml文件,
        // soap模式保存成特殊协议的xml文件,支持不序列化nonserialize标签的属性
        // xml模式不支持不序列化nonserialize标签的属性
        //

        //Creates a new TestSimpleObject object.
        TestSimpleObject obj = new TestSimpleObject();

        Console.WriteLine("Before serialization the object contains: ");
        obj.Print();

        //Opens a file and serializes the object into it in binary format.
        //Stream stream = File.Open("data_xml.xml", FileMode.Create);
        Stream stream = File.Open("data_soap.xml", FileMode.Create);

        SoapFormatter formatter = new SoapFormatter();

        //BinaryFormatter formatter = new BinaryFormatter();
        //XmlSerializer formatter = new XmlSerializer(typeof(TestSimpleObject));


        formatter.Serialize(stream, obj);
        stream.Close();

        //Empties obj.
        obj = null;

        //Opens file "data.xml" and deserializes the object from it.
        //stream = File.Open("data_xml.xml", FileMode.Open);
        stream = File.Open("data_soap.xml", FileMode.Open);

        formatter = new SoapFormatter();
        //formatter = new BinaryFormatter();
        //formatter = new XmlSerializer(typeof(TestSimpleObject));

        obj = (TestSimpleObject)formatter.Deserialize(stream);
        stream.Close();

        Console.WriteLine("");
        Console.WriteLine("After deserialization the object contains: ");
        obj.Print();
        Console.Read();
    }
Example #5
0
    void Start()
    {
        //Notice how you pass no parameter into this
        //extension method even though you had one in the
        //method declaration. The transform object that
        //this method is called from automatically gets
        //passed in as the first parameter
        print (2.828793e+00);

        //Creates a new TestSimpleObject object.
        TestSimpleObject obj = new TestSimpleObject();

        print("Before serialization the object contains: ");
        obj.Print();

        //Opens a file and serializes the object into it in binary format.
        Stream stream = File.Open("data.xml", FileMode.Create);
        BinaryFormatter formatter = new BinaryFormatter();

        //BinaryFormatter formatter = new BinaryFormatter();

        formatter.Serialize(stream, obj);
        stream.Close();

        //Empties obj.
        obj = null;

        //Opens file "data.xml" and deserializes the object from it.
        stream = File.Open("data.xml", FileMode.Open);
        formatter = new BinaryFormatter();

        //formatter = new BinaryFormatter();

        obj = (TestSimpleObject)formatter.Deserialize(stream);
        stream.Close();

         print("After deserialization the object contains: ");
        obj.Print();
        //		Console.WriteLine("you suck");
    }
Example #6
0
        /// <summary>
        /// Method for creating serialized collection by using SOAP format
        /// </summary>
        /// <returns></returns>
        public Stream CreateSerializedCollection()
        {
            //Creates a new TestSimpleObject object.
            TestSimpleObject obj = new TestSimpleObject();

            Console.WriteLine("Before serialization the object contains: ");
            obj.Print();

            //Opens a file and serializes the object into it in binary format.
            Stream        stream    = File.Open("data.xml", FileMode.Create);
            SoapFormatter formatter = new SoapFormatter();

            //BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(stream, obj);
            stream.Close();

            return(stream);
        }
Example #7
0
        /// <summary>
        /// Method for deserializing collections in SOAP format
        /// </summary>
        /// <param name="stream">serialized object in binary format that we want to deserialize</param>
        public void DeserializeCollection(Stream stream)
        {
            TestSimpleObject obj = new TestSimpleObject();

            //Empties obj.
            obj = null;

            //Opens file "data.xml" and deserializes the object from it.
            stream = File.Open("data.xml", FileMode.Open);

            SoapFormatter formatter = new SoapFormatter();

            formatter = new SoapFormatter();

            //formatter = new BinaryFormatter();

            obj = (TestSimpleObject)formatter.Deserialize(stream);
            stream.Close();

            Console.WriteLine("");
            Console.WriteLine("After deserialization the object contains: ");
            obj.Print();
        }