Solved

Object Self Serialization

Posted on 2010-08-22
5
658 Views
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...
0
Comment
Question by:mdbarker813
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
saragani earned 334 total points
Comment Utility
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);
            stream.Close();
        }

        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);
            stream.Close();

            return ret;
        }
0
 
LVL 11

Assisted Solution

by:saragani
saragani earned 334 total points
Comment Utility
Hi,
typeof(ProjectSerialization)

should be:
typeof(THE_TYPE_OF_YOUR_CLASS)
0
 
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 166 total points
Comment Utility
Hi,

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

Regards,
VSS

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
            fs.Close();  
        }
    }
}

Open in new window

0
 

Author Comment

by:mdbarker813
Comment Utility
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);
        }
    }
0
 

Author Closing Comment

by:mdbarker813
Comment Utility
Very close to what i was looking for.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now