Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using xml to save objects

Posted on 2005-03-21
2
Medium Priority
?
688 Views
Last Modified: 2013-11-19
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
Comment
Question by:dm14011
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Expert Comment

by:fromeroj
ID: 13597877
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
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 2000 total points
ID: 13598373
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question