This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

XML SERIALIZATION IN C#
(2 votes, average: 3.00 out of 5)
Written by Christopher   
Thursday, 12 June 2008 18:30

INTRODUCTION

Serialization is the process of persisting an object to disk. Another application can deserialize your object and it will be in the same state it was before the serialization. This tutorial talks about XML serialization. The namespace containing classes and methods suitable for such a serialization is System.Xml.Serialization.

XML SERIALIZATION

In order to serialize an object you must first create an XmlSerializer object. You must also create a stream that will write to or read from a file. Then, you call the appropriate method of serialization passing it the stream object you created. To deserialize an xml serialized object you created you simply call the deserialize method passing it the stream that reads from the xml document. The final step is to cast the object into the correct type.

When you need to exchange information with an application that is not based on the .Net framework you should use Xml serialization. Xml provides the following benefits over standar serialization techniques:

1. Greater interoperability: Xml is a text file based format and all modern operating systems and developing environments include libraries for processing such files.

2. Administrator friendly: by storing objects in xml format , it gives the administrators the opportunity to view and edit xml files. So, an administrator can easily modify your object or troubleshoot problems.

3. Better forward compatibility: xml-serialized objects are self-described. When you need to replace your application with a newer one, the transition will be straight forward.

 

SERIALIZE AND DESERIALIZE DATA

The basic steps for creating an Xml serialization are the following ones:

· Create a stream, Textwriter or XmlWriter object to store the serialized data.

· Create an XmlSerializer object. You need to pass the type of object you want to serialize.

· Call the XmlSerializer.Serialize() method to serialize the object. The produced data is extracted to the stream.

The following console application demonstrates these concepts. An object of DateTime is being serialized by storing its data to an xml file. Then, after some time, the user presses a key and the application retrieves the stored data. The information on the file is printed on the screen.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Xml.Serialization;

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

//Create file to save the data to

FileStream myStream = new FileStream("Output.XML", FileMode.Create);

// Create an XML serializer object

XmlSerializer myXs = new XmlSerializer(typeof(DateTime));

//Serialize the data

myXs.Serialize(myStream, System.DateTime.Now);

myStream.Close();

Console.WriteLine("Press a key to continue");

Console.ReadLine();

Console.WriteLine("The time is " + DateTime.Now.TimeOfDay.ToString());

//DESERIALIZE THE DATA

FileStream mySecondFS = new FileStream("Output.XML", FileMode.Open);

XmlSerializer mySecondXs = new XmlSerializer(typeof(DateTime));

DateTime StoredTime = (DateTime)mySecondXs.Deserialize(mySecondFS);

mySecondFS.Close();

Console.WriteLine("The time was " + StoredTime.TimeOfDay.ToString());

Console.ReadKey();

}

}

}

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options