C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,441 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,504 people online right now. Registration is fast and FREE... Join Now!




Serializing and object with XML and SOAP

 
Reply to this topicStart new topic

> Serializing and object with XML and SOAP

SixOfEleven
Group Icon



post 7 Apr, 2009 - 02:36 PM
Post #1


Sometimes you might want to write out an object using XML with out coding it by hand. Serializing is an easy way to do that. By serializing an object you create a static picture of that object at that moment. There are two ways to serialize an object. One way is binary serialization, the other is using XML and SOAP. Here I will discuss the later.

Using this form of serialization you can store the state of any public fields and properties of a public class. It will not store any other information. It will create a simple XML document.

Let's say that you have a class called Person. This the code for that class:

CODE

using System;
using System.Collections.Generic;
using System.Text;

namespace XMLandSOAP
{
    public class Person
    {
        // These will be serialized
        public string Name;
        public int Age;
        public string Email;
        public string City;

        // These will not be serialized
        private string socialSecurityNumber;
        private string bankAccountNumber;
        private string bankName;

        // This will be serialized        
        public string BankName
        {
            get { return bankName; }
            set { bankName = value; }
        }

        public Person()
        {
            Name = "Jane Doe";
            Age = 18;
            Email = "janedoe@ficticious.ca";
            City = "Perth";
            socialSecurityNumber = "1935469";
            bankAccountNumber = "193-98457";
            bankName = "Bank of Canada";
        }

    }
}


I don't know what an American Social Security number looks like. rolleyes.gif
To serialize this class you will need to add two using statements.

CODE

using System.Xml.Serialization;
using System.IO;


The first one adds the ability to serialize the object. The second is needed because you will need a stream to serialize the object.

The code to serialize the object is simple you can add it to the class. This is the code:

CODE

public void WritePerson()
{
    XmlSerializer person = new XmlSerializer();

    StreamWriter stream = new StreamWriter("Person.XML");
    person.Serialize(stream, this);
    stream.Close();
}


What the code does is create a XMLSerializer, then a StreamWriter, then it serializes the object and finally it closes the stream.

To retrieve the serialized document you use a process called deserialization, it is the reverse of serialization. Again, it is fairly simple. You can add this method to your class:

CODE

public Person ReadPerson()
{
    XmlSerializer person = new XmlSerializer();
    Person tempPerson = new Person();

    StreamReader stream = new StreamReader("Person.XML");
    tempPerson = (Person)person.Deserialize(stream);
    stream.Close();
    return tempPerson;
}


First you create an XmlSerializer. Then you need to create a temporary object to hold the object that is being deserialized. Then you create a StreamReader. The Deserialize method returns an object that you must cast. You close the stream and return the object.

This is a very simple way to save a public class and it's public fields and properties to a simple XML file.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 01:20AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month