Feb 3, 2011

.Net convert object to XML (Serialization)


.NET OBJECT SERIALIZATION

Introduction
Serialization can be defined as the process of storing the state of an object instance to a storage medium. During this process, the public and private fields of the object and the name of the class, including the assembly containing the class, is converted to a stream of bytes, which is then written to a data stream. When the object is subsequently deserialized, an exact clone of the original object is created.
When implementing a serialization mechanism in an object-oriented environment, you have to make a number of tradeoffs between ease of use and flexibility. The process can be automated to a large extent, provided you are given sufficient control over the process. For example, situations may arise where simple binary serialization is not sufficient, or there might be a specific reason to decide which fields in a class need to be serialized. The following sections examine the robust serialization mechanism provided with the .NET Framework and highlight a number of important features that allow you to customize the process to meet your needs.

XML SERIALIZATION

It is widely used when two different domains talk. I faced a same situation where i had to develop an .net application where it needs to talk to a java application. I was forced to a situation where i need to send a object from my application to the other application by calling the exe. I was wondering how is that possible then i came through something called serialization of objects.
I used xml serialization and then converted the my object to xml and passed the xml string to the other application as a argument. To know how to pass a string as a argument 


We will now see how the object is converted to xml serialization with an example
for example we have a class for which the object has to be sent.
public class MyObject
    {
        public string property1{ get; set; }
        public string property2 { get; set; }      
        public decimal property3 { get; set; }
        public int property4 { get; set; }      
    }

now we add values to all this properties
MyObject  myObject  =new MyObject();
myObject.Property1=sandeep;
myObject.Property2=naveen;
myObject.Property3=rajkumar;
myObject.Property1=rajendra

XmlSerializer xml = new XmlSerializer(myObject.GetType());
            string fileName =@ "C:/Test.xml";
            FileStream fileStream = new FileStream(fileName, FileMode.Create);
            xml.Serialize(fileStream, myObject);
Now go to the path specified you will find the object serialized into xml.

Hope this helped u..
CHEERS..
THESANDEEP
www.thesandeep.com


2 comments:

Anonymous said...

I really like your weblog.. extremely wonderful shades & theme. Did you create this internet site yourself? Plz reply back again as I’m looking to create my own website and would like to know wheere u acquired this from. many thanks

sandeep kumar said...

Dude. I have taken just a theme which is been provided by google, off course i have just modified accordingly.

You can develop yours more beautiful than this, you need to just have patience and skill to use asp.net.