Object Self Serialization

Posted on 2010-08-22
Last Modified: 2012-05-10
I have done this before, however I've been working in another area... I forgot in my old age...

I want to have a object that self serialize...
Question by:mdbarker813
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 11

Accepted Solution

saragani earned 334 total points
ID: 33498201
You mean like this?

       public void Save(string filename)
            XmlSerializer serializer = new XmlSerializer(typeof(THE_TYPE_OF_YOUR_CLASS));

            FileStream stream = File.Create(filename);
            serializer.Serialize(stream, this);

        public static THE_TYPE_OF_YOUR_CLASS Load(string filename)
            FileStream stream = File.Open(filename, FileMode.Open);
            XmlSerializer serializer = new XmlSerializer(typeof(ProjectSerialization));
            THE_TYPE_OF_YOUR_CLASS ret = (THE_TYPE_OF_YOUR_CLASS)serializer.Deserialize(stream);

            return ret;
LVL 11

Assisted Solution

saragani earned 334 total points
ID: 33498212

should be:
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 166 total points
ID: 33498221

See the code for Serialization of the object. Modify it or let me know how you want to do that.


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;

namespace mcts_C5_L1_Serialization_Deserialization
    class Program
        static void Main(string[] args)
            string data = "Hello World!"; 

            // Create file to save the data to
            FileStream fs = new FileStream(@"D:\Zodes\Serialized.Data", FileMode.Create);

            // Create a BinaryFormatter object to perform the serialization
            // Specifically for the client having .NET Application
            BinaryFormatter bf = new BinaryFormatter();

            // Use the BinaryFormatter object to serialize the data to the file
            bf.Serialize(fs, data);

            // Close the file

Open in new window


Author Comment

ID: 33665688
Sorry guys, I was out for a while. While away I had time to figure out what I wanted to to. Here is what i was looking for. I already knew how to serialize and deserialize and object. I just wanted to make it a cleaner process.

public class Serializer<ObjectToSerialize>
        public static ObjectToSerialize Load(string xml)
            return (ObjectToSerialize)Utilities.Deserialize(xml, typeof(ObjectToSerialize));  

        public string ToXML()
            return Utilities.Serialize(this);

Author Closing Comment

ID: 33665720
Very close to what i was looking for.

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question