Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Serializing and deserializing from a base class
Message
From
11/09/2008 14:40:14
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Serializing and deserializing from a base class
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01346769
Message ID:
01346769
Views:
47
Hi all,

I need to serialize some entity objects and deserialize them later. I had thought it would be slick if I could do this from the base class so any of they different entities (types) could use the same code. I ran into a couple of issues, so bugs in this code. I am not really up how to do this when working with different types generically. Does someone have an idea on the LoadFromQueue method of have below. I have put this in the base class for all entity objects in the project.

I took out all the extra code like catch
Thanks
Tim
public void SaveToQueue(string queuePath, string spotId)
{
     string saveName = "Spot_" + spotId + ".xml";
     try
     {
	ABusinessEntity.Serialize(this, saveName);
	Debug.WriteLine("Saved", "Queue Item: " + saveName);
     }
}

public bool LoadFromQueue(string queuePath, string fileName)
{
     bool llLoaded = false;
     ABusinessEntity oEntity;
     try
     {
          if (File.Exists(queuePath + fileName))
          {
               oEntity = ABusinessEntity.Deserialize(queuePath + fileName, typeof(TrackSpotEntity));
               llLoaded = true;
          }
     }
     return llLoaded;
}

private static void Serialize(ABusinessEntity entity, String fileName)
{
     try
     {
          FileStream file = File.Create(fileName);
          XmlSerializer xml = new XmlSerializer(entity.GetType());
          xml.Serialize(file, entity);
          file.Close();
     }
}

private static object Deserialize(String fileName, Type objType)
{
     object stuff = null;
     try
     {
          FileStream file = File.Open(fileName, System.IO.FileMode.Open);
          XmlSerializer xml = new XmlSerializer(objType);
          stuff = xml.Deserialize(file);
          file.Close();
     }
     return stuff;
}
Timothy Bryan
Next
Reply
Map
View

Click here to load this message in the networking platform