Voorbeeld gebruik XmlSerializer class to serialize XML in objects en de-serialize objects in XML

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;

// settingsBit specific usings
using System.Xml;
using System.IO;
using System.Xml.Serialization;

using System.Web;
using System.ComponentModel;


namespace InfoReports.BIT.LogicBIT.Settings
{
    [DefaultPropertyAttribute("Settings")]
    public class clsSettings:IDisposable 
    {
        #region - privates -
        private static clsSettings Settings;
        private XmlSerializer xs;
        private TextWriter tw;
        private TextReader tr;
        private string fn = @"C:\Program Files\InfoReports Business Intelligence\BIT\settings.xml";
        private string fn64 = @"C:\Program Files (x86)\InfoReports Business Intelligence\BIT\settings.xml";

        private static string strBitPath = @"C:\Program Files\InfoReports Business Intelligence\BIT\settings.xml";
        private static string strBitPath64 = @"C:\Program Files (x86)\InfoReports Business Intelligence\BIT\settings.xml";

        #endregion

        #region - propertys -
        

        private List lstExtractSettings;
        [CategoryAttribute("Sharepoint Instellingen"), DescriptionAttribute("Sharepoint instellingen per omgeving")]
        [DisplayName ("Sharepoint instellingen")]
        public List LstExtractSettings
        {
            get {
                if (this.lstExtractSettings == null )
                {
                    this.lstExtractSettings = new List();
                }
                return lstExtractSettings; }
            set { lstExtractSettings = value; }
        }

        private Boolean bron;
        [CategoryAttribute("Datasource Bron instellingen"), DescriptionAttribute("Indien True wordt username / Password gewijzigd op de Bron datasource")]
        public Boolean Bron
        {
            get { return bron; }
            set { bron = value; }
        }

        private string usernameBron;
        [CategoryAttribute("Datasource Bron instellingen"), DescriptionAttribute("Username voor datasource Bron")]
        public string UsernameBron
        {
            get { return usernameBron; }
            set { usernameBron = value; }
        }

        private string passwordBron;
        [CategoryAttribute("Datasource Bron instellingen"), DescriptionAttribute("Password voor datasource Bron")]
        public string PasswordBron
        {
            get { return passwordBron; }
            set { passwordBron = value; }
        }
        private string inforeportsServer;
        [CategoryAttribute("Algemene instellingen"), DescriptionAttribute("De hostname van de server waarop de InfoReports database is geinstalleerd")]
        public string InforeportsServer
        {
            get { return inforeportsServer; }
            set { 
                inforeportsServer = value;  }
        }

        [CategoryAttribute("Algemene Instellingen"), DescriptionAttribute("Password waarmee package ge-encrypt worden. Hoeft nog niet ingevuld worden !")]
        private string password;
        [DisplayName("Password")]
        public string Password
        {
            get { return password; }
            set { password = value;}
        }

        
        private string reportsWebService;
        [CategoryAttribute("Algemene instellingen"), DescriptionAttribute("Report Server URL")]
        public string ReportsWebservice
        {
            get { return reportsWebService; }
            set { 
                reportsWebService = value;
            }
        }

        private string wslistextracturl;
        [Browsable(false)]
        public string WSlistExtractUrl
        {
            get { return wslistextracturl; }
            set {
                wslistextracturl = value;
            }
        }

        private string wslistextractlocation;
        [Browsable(false)]
        public string WSlistExtractLocation
        {
            get { return wslistextractlocation; }
            set { 
                wslistextractlocation = value;
            }
        }

        private string wslistextractsharepoint;
        [Browsable(false)]
        public string WSlistExtractSharepoint
        {
            get { return wslistextractsharepoint; }
            set { wslistextractsharepoint = value;
        }
        }

        
        private string reportserversource;
        [CategoryAttribute("Algemene instellingen"), DescriptionAttribute("De hostname van de server waarvandaan de reports gekopieerd worden")]
        public string ReportServerSource
        {
            get { return reportserversource; }
            set { 
                reportserversource = value;
            }
        }

        
        private string reportserverdestination;
        [CategoryAttribute("Algemene instellingen"), DescriptionAttribute("De hostname van de server waar de reports naar toe gekopieerd worden")]
        public string ReportServerDestination
        {
            get { return reportserverdestination; }
            set { 
                reportserverdestination = value;
        }
        }


        private bool  ssl;
        [CategoryAttribute("Algemene instellingen"), DescriptionAttribute("SSL")]
        public bool  SSL
        {
            get { return ssl; }
            set { 
                ssl = value;
            }
        }

        private string environmentDest;
        [CategoryAttribute("Omgevingen"), DescriptionAttribute("De omgeving waar de reports naar toe gekopieerd worden")]
        [DisplayName("Doel omgeving")]
        public string EnvironmentDest
        {
            get { return environmentDest; }
            set { environmentDest = value; }
        }

        private string environmentStart;
        [CategoryAttribute("Omgevingen"), DescriptionAttribute("De omgeving waarvandaan de reports gekopieerd worden")]
        [DisplayName("Start omgeving")]
        public string EnvironmentStart
        {
            get { return environmentStart; }
            set { environmentStart = value; }
        }

        #endregion

        #region - contstructor -
        private clsSettings()
        {
            getFileName();
        }
        #endregion

        #region - methods -

        public void UpdateSettingsBit(DataTable dt)
        {

            using (DataBIT.clsBitSettings settings = new DataBIT.clsBitSettings (this.inforeportsServer ))
            {
                settings.UpdateSettingsBit(dt);
            }
        }

        /// gets the current working directory
        /// 
        /// applicaction path of the application for static deserialize method
        private static string getFN()
        {
            DirectoryInfo di = new DirectoryInfo (Path.GetDirectoryName (strBitPath ));
            DirectoryInfo di64 = new DirectoryInfo (Path.GetDirectoryName (strBitPath64 ));
            FileInfo fi = new FileInfo(strBitPath);
            FileInfo fi64 = new FileInfo(strBitPath64);

            if (di.Exists)
                if (fi.Exists)
                    return strBitPath;
                else
                    return string.Empty;
            if (di64.Exists)
                if (fi64.Exists)
                    return strBitPath64;
                else
                    return string.Empty;
            else
                return string.Empty;
        }

        /// 
        /// Get the filename
        /// 
        private string getFileName()
        {
            DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName (fn));
            DirectoryInfo di64 = new DirectoryInfo(Path.GetDirectoryName(fn64));
            if (di.Exists)
                    return fn;
                if (di64.Exists)
                    return fn64;
                else
                    return string.Empty;
        }


        /// 
        /// Serializes current class to XML
        /// 
        public void SerializeSettings()
        {
            xs = new XmlSerializer(this.GetType());
            if (!string.IsNullOrEmpty(this.getFileName()))
            {
                using (tw = new StreamWriter(this.getFileName()))
                {
                    
                    xs.Serialize(tw, this);
                }
            }
        }

        // Builds the connections string from the settings
        public string GetConnectionString()
        {
            return ""; // "Data source=" + this.strdatabaseserver + ";Initial Catalog=InfoReports;User id = " + this.struserid + "; password=" + this.strpassword;
        }


        /// Deserialized settings from XML
        public static clsSettings DeserializeSettings()
        {
            if (Settings == null)
            {
                // If there is a XML file deserilize it in al new object
                if (!string.IsNullOrEmpty(getFN()))
                {
                    XmlSerializer xs = new XmlSerializer(new clsSettings().GetType());
                    using (TextReader tr = new StreamReader(getFN()))
                    {
                        Settings = (clsSettings)xs.Deserialize(tr);
                    }
                }
                // If ther is not a XML file just returns new object
                else
                {
                    Settings = new clsSettings();
                }
            }
            return Settings;
        }

        public void Dispose()
        {
            this.SerializeSettings();
            
            this.tw.Dispose();
        }
        #endregion
    }
}

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!