Results 1 to 2 of 2

Thread: [RESOLVED] Encapsulating Serialization into a class

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    Resolved [RESOLVED] Encapsulating Serialization into a class

    I have a class, see below, and I want to provide methods to Save/Load the class to disk using XML serialization.

    I got the Save portion working, but I am having issues with the Load. I am not quite sure how to have a class deserialize itself.

    The main application will create an instance of the CSLInfo class and call the Save/Load methods at various points.

    To save data I would do the following:

    Code:
    CSLInfo csl = new CSLInfo();
    
    csl.Sites.Add(new Site("Customer1", "customer1", true));
    csl.Sites.Add(new Site("Customer2", "customer2", false));
    csl.Save();
    And to load data I would do the following:

    Code:
    CSLInfo csl = new CSLInfo();
    
    csl.Load();
    I would like the Load method to read the xml file from disk and populate the csl variable with the data. I'm not sure if this even possible though.

    Also, I read up on implementing ISerializable, but I think I'd have to manually load all the data using ISerializable.GetObjectData, but that's more work than I want to put into this small application.

    My final option would be to move the serialization/deserialization code to methods in the main application.

    Any input on this would be appreciated.

    Thanks,
    CT

    ==== MY CLASS

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Runtime.Serialization;
    
    namespace Sandbox
    {
        [Serializable]
        public class CSLInfo
        {
            public CSLInfo() { }
            
            List<Site> _sites = new List<Site>();
    
            public List<Site> Sites
            {
                get { return _sites; }
                set { _sites = value; }
            }
    
            public void Load()
            {
                
            }
    
            public void Save()
            {
                Stream s = null;
    
                try
                {
                    s = File.Create(@"csl.xml");
                    XmlSerializer serial = new XmlSerializer(typeof(CSLInfo));
                    serial.Serialize(s, this);
                }
                finally
                {
                    if (s != null)
                        s.Dispose();
                }
            }
        }
    
        public class Site
        {
            private string _customerName = "";
            private string _dbName = "";
            private bool _hasWebsite = false;
    
            public Site() { }
    
            public Site(string customerName, string databaseName, bool hasWebsite)
            {
                _customerName = customerName;
                _dbName = databaseName;
                _hasWebsite = hasWebsite;
            }
    
            public string CustomerName
            {
                get { return _customerName; }
                set { _customerName = value; }
            }
    
            public string DatabaseName
            {
                get { return _dbName; }
                set { _dbName = value; }
            }
    
            public bool HasWebsite
            {
                get { return _hasWebsite; }
                set { _hasWebsite = value; }
            }
        }
    }

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    Re: Encapsulating Serialization into a class

    Well, it never fails. After hours Googling and finally posting a question here I found my answer online. /sigh

    For any that are interested my Load method ended as follows:

    Code:
    public static CSLInfo Load()
            {
                FileStream s = null;
                CSLInfo t = null;
    
                try
                {
                    s = File.OpenRead(@"csl.xml");
                    XmlSerializer serial = new XmlSerializer(typeof(CSLInfo));
                    t = (CSLInfo)serial.Deserialize(s);
                }
                finally
                {
                    if (s != null)
                        s.Dispose();
                }
    
                return t;
            }
    In the main program I do the following:

    Code:
    CSLInfo csl = CSLInfo.Load();
                
                foreach(Site s in csl.Sites)
                    MessageBox.Show(s.CustomerName);
    Sorry for the posts, but maybe it'll help someone else.

    CT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width