ConfigSerializer.cs

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace PacMon
{
    /// <summary>ApplicationSettings Xml serialization utility.</summary>
    public class ApplicationSettingsSerializer
    {
        private static string file = Directory.GetCurrentDirectory() + @"\config.xml";
        private XmlSerializer s = null;
        private Type type = null;

        /// <summary>Default constructor.</summary>
        public ApplicationSettingsSerializer()
        {
            this.type = typeof(ApplicationSettings);
            this.s = new XmlSerializer(this.type);
        }

        /// <summary>Deserializes to an instance of ApplicationSettings.</summary>
        /// <param name="xml">String xml.</param>
        /// <returns>ApplicationSettings result.</returns>
        public ApplicationSettings Deserialize(string xml)
        {
            TextReader reader = new StringReader(xml);
            return Deserialize(reader);
        }

        /// <summary>Deserializes to an instance of ApplicationSettings.</summary>
        /// <param name="doc">XmlDocument instance.</param>
        /// <returns>ApplicationSettings result.</returns>
        public ApplicationSettings Deserialize(XmlDocument doc)
        {
            TextReader reader = new StringReader(doc.OuterXml);
            return Deserialize(reader);
        }

        /// <summary>Deserializes to an instance of ApplicationSettings.</summary>
        /// <param name="reader">TextReader instance.</param>
        /// <returns>ApplicationSettings result.</returns>
        public ApplicationSettings Deserialize(TextReader reader)
        {
            ApplicationSettings o = (ApplicationSettings)s.Deserialize(reader);
            reader.Close();
            return o;
        }

        /// <summary>Serializes to an XmlDocument.</summary>
        /// <param name="ApplicationSettings">ApplicationSettings to serialize.</param>
        /// <returns>XmlDocument instance.</returns>
        public XmlDocument Serialize(ApplicationSettings applicationsettings)
        {
            string xml = StringSerialize(applicationsettings);
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.LoadXml(xml);
            doc = Clean(doc);
            return doc;
        }

        private string StringSerialize(ApplicationSettings applicationsettings)
        {
            TextWriter w = WriterSerialize(applicationsettings);
            string xml = w.ToString();
            w.Close();
            return xml.Trim();
        }

        private TextWriter WriterSerialize(ApplicationSettings applicationsettings)
        {
            TextWriter w = new StringWriter();
            this.s = new XmlSerializer(this.type);
            applicationsettings.xmlns = new XmlSerializerNamespaces();
            applicationsettings.xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            applicationsettings.xmlns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
            s.Serialize(w, applicationsettings);
            w.Flush();
            return w;
        }

        private XmlDocument Clean(XmlDocument doc)
        {
            doc.RemoveChild(doc.FirstChild);
            XmlNode first = doc.FirstChild;
            foreach (XmlNode n in doc.ChildNodes)
            {
                if (n.NodeType == XmlNodeType.Element)
                {
                    first = n;
                    break;
                }
            }
            if (first.Attributes != null)
            {
                XmlAttribute a = null;
                a = first.Attributes["xmlns:xsd"];
                if (a != null) { first.Attributes.Remove(a); }
                a = first.Attributes["xmlns:xsi"];
                if (a != null) { first.Attributes.Remove(a); }
            }
            return doc;
        }

        /// <summary>Reads config data from config file.</summary>
        /// <param name="file">Config file name.</param>
        /// <returns></returns>
        public static ApplicationSettings ReadFile()
        {
            ApplicationSettingsSerializer serializer = new ApplicationSettingsSerializer();
            try
            {
                string xml = string.Empty;
                using (StreamReader reader = new StreamReader(file))
                {
                    xml = reader.ReadToEnd();
                    reader.Close();
                }
                return serializer.Deserialize(xml);
            }
            catch { }
            return new ApplicationSettings();
        }

        /// <summary>Writes config data to config file.</summary>
        /// <param name="file">Config file name.</param>
        /// <param name="config">Config object.</param>
        /// <returns></returns>
        public static bool WriteFile(ApplicationSettings config)
        {
            bool ok = false;
            ApplicationSettingsSerializer serializer = new ApplicationSettingsSerializer();
            try
            {
                string xml = serializer.Serialize(config).OuterXml;
                using (StreamWriter writer = new StreamWriter(file, false))
                {
                    writer.Write(xml);
                    writer.Flush();
                    writer.Close();
                }
                ok = true;
            }
            catch { }
            return ok;
        }
    }
}