Exemplo n.º 1
0
        private void btnXmlSerialization_Click(object sender, EventArgs e)
        {
            // * You use the XmlSerializer class in the System.Xml.Serialization namespace.
            // * One difference between the XmlSerializer and BinaryFormatter is that the XmlSerializer serializes only public properties and fields.
            // * You also do not need to use the[Serializable] attribute when declaring the class. 
            // * Also, the class must be public. 

            var person = new Person();
            person.FirstName = "Cassio";
            person.LastName = "Vinicius";
            person.SetId(1);

            var xmlSerializerWriter = new XmlSerializer(typeof(Person));
            var streamWriter = new StreamWriter("Person.xml");
            xmlSerializerWriter.Serialize(streamWriter, person);
            streamWriter.Close();

            var xmlSerializerReader = new XmlSerializer(typeof(Person));
            var fileStream = new FileStream("Person.xml", FileMode.Open);
            var personRead = (Person)xmlSerializerReader.Deserialize(fileStream);
            fileStream.Close();


            //var xmlSerializer = new XmlSerializer(typeof(Person));
            //var streamWriter = new StreamWriter("Person.xml");
            //xmlSerializer.Serialize(streamWriter, person);
            //streamWriter.Close();

            ////The code produces the following file:
            ////<? xml version="1.0" encoding="utf-8"?>
            ////<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            ////    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            ////  <FirstName>Joe</FirstName>
            ////  <LastName>Smith</LastName>
            ////</Person>

            ////            If you want to ignore a property, use the[XmlIgnore] attribute before the property. The following
            ////code would ignore the FirstName property when serializing the object to XML:
            ////[XmlIgnore]
            ////public string FirstName;

            //// You can use the following code to read the XML back into an object:
            //XmlSerializer xmlSerializer2 = new XmlSerializer(typeof(Person));
            //FileStream fs = new FileStream("Person.xml", FileMode.Open);
            //Person person2 = (Person)xmlSerializer.Deserialize(fs);
            //fs.Close();
        }
Exemplo n.º 2
0
        private void BinarySerialization_Overview()
        {
            // * The BinaryFormatter object is used to serialize and deserialize an object.
            // * This is found in the System.Runtime.Serialization.Formatters.Binary namespace.
            // * The two main methods you need to be concerned about are the Serialize and Desearialize methods.
            // * You can use the BinaryFormatter along with a FileStream to read and write your objects to disk. 
            // * Remember, the FileStream is an object used to read and write data to and from disk as byte arrays. 
            // * For a class to be serialized, you must add the[Serializable] attribute to the top of the class. 
            // * The following example creates a class called Person and makes it serializable:


            // [Serializable]
            // class Person
            // {
            //     private int _id;
            //     public string FirstName;
            //     public string LastName;
            //     public void SetId(int id)
            //     {
            //         _id = id;
            //     }
            // }

            // If you want to persist this object’s data to storage, you can create an instance of the BinaryFormatter
            // object and call its Serialize method.

            var person = new Person();
            person.SetId(1);
            person.FirstName = "Joe";
            person.LastName = "Smith";

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream("Person.bin", FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, person);
            stream.Close();



            // Be aware that even the private field will be persisted to the disk.You can restore the state of the
            // object by reading the Person.bin file and deserializing the file.


            stream = new FileStream("Person.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
            var person2 = (Person)formatter.Deserialize(stream);
            stream.Close();




            //If you execute the code and view the person2 object in the Watch window, you can see that all the
            //fields retain their value, even the private _id field.If you want to prevent the private field from being
            //persisted, you can add the[NonSerialized] attribute before the field declaration.
            //[Serializable]
            //class Person
            //        {
            //            [NonSerialized]
            //            private int _id;
            //            public string FirstName;
            //            public string LastName;
            //            public void SetId(int id)
            //            {
            //                _id = id;
            //            }
            //        }
            //        When serializing the object, the _id field will be skipped.If you were to run the code again and
            //        serialize the object and deserialize the object and view person2 in the Watch window, you would
            //        notice the _id is 0.

        }