Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Serializing and deserializing from a base class
Message
De
11/09/2008 14:40:14
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Serializing and deserializing from a base class
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Divers
Thread ID:
01346769
Message ID:
01346769
Vues:
48
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
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform