Let’s do Serialization!


Let’s do Serialization!

Welcome to CodeSpread!

Felt like sharing some code related to serialization. Lets keep the definition for some future articles and only code today. :)

Reading a XML file.

  • We have a XML file like Books.xml

<?xml version="1.0" encoding="utf-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  • We created a corresponding class for this XML file called Books.

public class Books
public string Name { get; set; }

public int ISBN { get; set; }

  • Add these namespaces.

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

  • Now, we have to create an instance of XML Serializer class of type “Books”. We require streamreader object to hold the stream. Then, we pass this stream to the Deserialize method of serializer class and assign it to Books object.

class Program
static void Main(string[] args)
Books book = new Books();
XmlSerializer serializer = new XmlSerializer(typeof(Books));
StreamReader reader = new StreamReader(@"G:\Books.xml");

book = (Books)serializer.Deserialize(reader);

Writing a XML file.

We will keep the same Books class and will write to it.

  • We create a XMLwriter object and pass two parameters; stream and books object to Serialize method of writer class.

class Program
static void Main(string[] args)

Books book = new Books();
book.name = "CodeSpread";
XmlSerializer writer = new XmlSerializer(typeof(Books));
StreamWriter file = new StreamWriter(@"G:\Books.xml");
writer.Serialize(file, books);



Just Code!

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On


  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • Note: GZipStreamNote: GZipStreamA short note from my experience: From the name itself you can at least make a guess about the functionality of the GZipStream class(present under: System.IO.Compression). Yes..your guess is abso...
  • VB.Net: Download file from FTP and UnZipVB.Net: Download file from FTP and UnZipExperience: Many application requires data migration tasks to execute on daily basis. To achieve this we generally seek help of SSIS package, if at all we are making use of SQL sever. ...
  • Styles of Source Code Comments in C#, SQL, XML, HTML, CSS, JavaScriptStyles of Source Code Comments in C#, SQL, XML, HTML, CSS, JavaScriptDevelopers take months to write beautiful pieces of code and reviewer gets only few hours or I say, minutes to provide suggestions and improvisations. Then it becomes a hard deal for a developer to...
  • C#: Useful JSON in .NetC#: Useful JSON in .NetWhy JSON? All web applications revolve around large chunk of data and availability of that data to our application is of utmost importance. To do that, we have options like fetch data from databas...
  • Simply LINQ: What,Why and How.Simply LINQ: What,Why and How.What is LINQ? LINQ is Language-Integrated Query (LINQ). It is primarily an extension to the language's capability where it adds query expressions which processes objects and data. It is a one s...
  • Interview Question SeriesInterview Question SeriesWe have shared our experiences with our readers from quite sometime. Today, when one of our readers asked us to write some interview questions then we realized that we should take an initiative and...
  • Tip : Code Structure C#Tip : Code Structure C#As a developer, I always stress on developing a habit of following the best practices to structure the code. It helps me to easily navigate through the code and also saves a lot of effort in search...
  • SQL:Target a TriggerSQL:Target a TriggerI remember those days when I used to simply fall into the trap set by client requirements which could have been easily resolved by using a trigger. So I can definitely say, trigger is a life saver,...