Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 12. Persistent Objects: Serializ... > Example Code Used in This Chapter

Example Code Used in This Chapter

The following code is presented in C# .NET and VB .NET. These examples correspond to the Java code that is listed inside the chapter itself.

The Person Class Example: C# .NET

// Class Person
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace CSSerial
{
    [XmlRoot("person")]
    public class Person
    {
        private String strName;
        private int intAge;
        private int intScore;

        public Person()
        {
            this.Name = "John Doe";
            this.Age=25;
            this.Score=50;
        }

        public Person(String name, int age, int score)
        {
            this.Name = name;
            this.Age = age;
            this.Score = score;
        }
        [XmlAttribute("name")]
        public String Name
        {
            get
            {
                return this.strName;
            }
            set
            {
                if (value == null) return;
                this.strName = value;
            }
        }

        [XmlElement("age")]
        public int Age
        {
            get
            {
                return this.intAge;
            }
            set
            {
                this.intAge = value;
            }
        }

        [XmlIgnore()]
        public int Score
        {
            get
            {
                return intScore;
            }
            set
            {
                this.intScore = value;
            }
        }
    }
}

// Class CSSerial
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace CSSerial
{
    class Program
    {
        static void Main(string[] args)
        {
            Program myProgram = new Program();
        }

        public Program()
        {
            Serialize();
            DeSerialize();
        }

        public void Serialize()
        {
            Person[] myPeople = new Person[3];
            myPeople[0] = new Person("John Q. Public", 32, 95);
            myPeople[1] = new Person("Jacob M. Smith", 35, 67);
            myPeople[2] = new Person("Joe L. Jones", 65, 77);
            XmlSerializer mySerializer = new XmlSerializer(typeof(Person[]));
            TextWriter myWriter = new StreamWriter("person.xml");
            mySerializer.Serialize(myWriter, myPeople);
            myWriter.Close();
        }

        public void DeSerialize()
        {
            Person[] myRestoredPeople;
            XmlSerializer mySerializer = new XmlSerializer(typeof(Person[]));
            TextReader myReader = new StreamReader("person.xml");
            myRestoredPeople = (Person[])mySerializer.Deserialize(myReader);
            Console.WriteLine("My People restored:");
            foreach (Person listPerson in myRestoredPeople)
            {
                Console.WriteLine(listPerson.Name + " is " + listPerson.Age + " years old.");
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

					  


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint