• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 692
  • Last Modified:

using xml to save objects

Hi,
I've heard that xml is the fastest way to save objects.
Please could you write some code including namespace to demonstrate
how to save an object and simple datatypes to a file
and then load them again.
thanks
0
dm14011
Asked:
dm14011
1 Solution
 
fromerojCommented:
imagine you have a small class let say
Person:

using System;
namespace Serializacion
{
      [Serializable()] //this attribute says the class will be serialized.
      public class Person
      {
            private string name;
            private string lastName;
            [NonSerialized()] public int age;//this will NOT be serialized
            public Bitmap Picture;

              public Person(string Name, string LastName,int age)
            {
                       this.age=age;
                  this.name= Name;
                  this.lastName= LastName;
            }
            public int Age
            {
                  get{return age;}
            }
            public string Name
            {
                  get{return name;}
            }
            public string LastName
            {
                  get{return lastName;}
            }
      }
}

so :
And you want to serialize it:

int age=13;
Person p=new Person("name","lastName",age)
tream oStream = File.Create("filename.xml");
SoapFormatter oSoapFormatter = new SoapFormatter();
oSoapFormatter.Serialize(oStream, p);
oStream.Close();

And to desserialize:

Stream oStream = File.OpenRead("filename.xml");
SoapFormatter oSoapFormatter = new SoapFormatter();
Person p= (Person)oSoapFormatter.Deserialize(oStream);
0
 
Thandava VallepalliCommented:
Here is the complete code .... but give points to fromeroj......


======================
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization;
using System.IO;
using System;

namespace Serializacion
{
     [Serializable()]             //this attribute says the class will be serialized.
     public class Person
     {
          private string name;
          private string lastName;
          [NonSerialized()] public int age;            //this will NOT be serialized

          public Person(string Name, string LastName,int age)
          {
               this.age=age;
               this.name= Name;
               this.lastName= LastName;
          }
          public int Age
          {
               get{return age;}
          }
          public string Name
          {
               get{return name;}
          }
          public string LastName
          {
               get{return lastName;}
          }
             
              public static void Main()
              {
                        int age=13;
                        Person p=new Person("RaviSanker","Thandava",age);
                        Stream oStream = File.Create("filename.xml");
                        SoapFormatter oSoapFormatter = new SoapFormatter();
                        oSoapFormatter.Serialize(oStream, p);
                        oStream.Close();
                        
                        //And to desserialize:
                        oStream = File.OpenRead("filename.xml");
                        oSoapFormatter = new SoapFormatter();
                        p= (Person)oSoapFormatter.Deserialize(oStream);
                        Console.WriteLine( p.LastName );
              }
     }
}
================================


itsvtk
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now