Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Saturday, May 29, 2010

XML Serialization and Deserialization Simple example



Things you should remember:
1. Xml Serialization or De-Serialization needs a parameterless constructor, if you want to serialize an object.
2. Make the class Serializable using [Serializable] before class definition.
3. You need to add reference of  System.Xml.Serialization namespace.
4. Create Xml Serializer object like this:
    XmlSerializer xs = new XmlSerializer(typeof(Sometype));
    Note: Sometype means you have to declare some type like this XmlSerializer xs = new             XmlSerializer(typeof(String)) or XmlSerializer xs = new XmlSerializer(typeof(Object)) etc
5. You can serialize any object like this:
     xs.Serialize(fs1, Object);

Okay if you are new to serialization, i would suggest you check these examples first and then jump to this one:
http://mctsexam70-536tutorials.blogspot.com/2010/05/how-to-serialize-and-de-serialize.html
http://mctsexam70-536tutorials.blogspot.com/2010/05/how-to-serialize-and-de-serialize_24.html
http://mctsexam70-536tutorials.blogspot.com/2010/05/creating-serilizable-class-and-writing.html
http://mctsexam70-536tutorials.blogspot.com/2010/05/soap-serialization-example-using.html


Now am gonna believe that you have checked the previous posts to learn the basics of serialization, so am not gonna stress on details. First i will create a class called Item (Yes! Again, I know) and am going to define a parameterless function so that i can perform xml serialization and deserialization. Go ahead do it:

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

namespace ConsoleApplication1
{
   
        [Serializable]
        public class Item
        {
            public string name;
            public double price;
            public Item()
            {
                name = "Product_Name";
                price = 21;

            }

            public Item(string _name, double _price)
            {
                name = _name;
                price = _price;
            }

            public override string ToString()
            {
                return "The Item " + name + " would cost you " + price.ToString();
            }

        }
   
}

Now start serialization in your program.cs file which is created with your console application as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace XmlSerialization
{
    class Program
    {
        static void Main(string[] args)
        {
            Item Item1 = new Item("MCTS Tony Northrup Book", 55);
            Item Item2 = new Item("MCTS Braindumps Material", 159.99);
            Item Item3 = new Item("MCTS 70-528 Amit Kalani Book", 68.99);
            List ItemsList = new List();
            ItemsList.Add(Item1);
            ItemsList.Add(Item1);
            ItemsList.Add(Item1);
            FileStream fs = new FileStream(@"C:\Test.Sam", FileMode.Create);
            //You can write it to any file you want, if you see i have created a file of type.Sam, even you can make //a file with your name extension
            XmlSerializer xs = new XmlSerializer(typeof(List));
            xs.Serialize(fs, ItemsList);
            Console.WriteLine("Written items to the given file");

        }
    }
}
 

Free MCTS EXAM 70-536 training tutorials on this website, soon will be launching video tutorials for all the same topics 



Check out the video, if you still have any doubts on this:

No comments:

Post a Comment