Click here to Skip to main content
15,879,326 members
Articles / Multimedia / GDI+

Drawing Library

Rate me:
Please Sign up or sign in to vote.
4.78/5 (63 votes)
10 Dec 2007CPOL3 min read 292K   12.8K   211  
A library for creating shapes and developing tools.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace Globe.Xml.Serialization
{
    /// <summary>
    /// Reads an SerializableData class and creates a xml file.
    /// </summary>
    public class XmlSerializeWriter
    {
        #region Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        public XmlSerializeWriter()
        {
        }

        #endregion

        #region Properties

        XmlDocument _xmlDocument = new XmlDocument();
        /// <summary>
        /// XmlDocument that contains the DOM struct.
        /// </summary>
        public XmlDocument XmlDocument
        {
            get { return _xmlDocument; }
            set { _xmlDocument = value; }
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Writes an xml file.
        /// </summary>
        /// <param name="fileName">Xml file to write.</param>
        /// <param name="serializableData">SerializableData to read.</param>
        public void WriteXml(string fileName, SerializableData serializableData)
        {
            CreateXmlDeclaration();

            WriteXml(_xmlDocument, serializableData);
            
            _xmlDocument.Save(fileName);
        }

        #endregion

        #region Protected Functions

        /// <summary>
        /// Writes an xml node.
        /// </summary>
        /// <param name="xmlNode">Xml node.</param>
        /// <param name="serializableData">SerializableData to write in the XmlNode.</param>
        virtual protected void WriteXml(XmlNode xmlNode, SerializableData serializableData)
        {
            XmlElement xmlElementClass = null;

            try
            {
                xmlElementClass = _xmlDocument.CreateElement(GetFormattedText(serializableData.TagName));
//                xmlElementClass = _xmlDocument.CreateCDataSection(serializableData.TagName);
            }
            catch
            {
                throw new XmlSerializationException(_xmlDocument, serializableData);
            }
            
            xmlElementClass.Attributes.Append(CreateXmlAttribute("value", serializableData.Value));
            xmlElementClass.Attributes.Append(CreateXmlAttribute("type", serializableData.Type));
            xmlElementClass.Attributes.Append(CreateXmlAttribute("assembly", serializableData.Assembly));
            xmlElementClass.Attributes.Append(CreateXmlAttribute("assemblyQualifiedName", serializableData.AssemblyQualifiedName));
            xmlElementClass.Attributes.Append(CreateXmlAttribute("name", serializableData.FieldName));
            
            xmlNode.AppendChild(xmlElementClass);

            foreach (SerializableData data in serializableData.SerializableDataCollection)
                WriteXml(xmlElementClass, data);
        }

        /// <summary>
        /// Create the XmlDeclaration on top of the xml file.
        /// </summary>
        virtual protected void CreateXmlDeclaration()
        {
            XmlDeclaration xmlDeclaration = _xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", "no");
            _xmlDocument.AppendChild(xmlDeclaration);
        }

        /// <summary>
        /// Create an XmlAttribute for an XmlNode.
        /// </summary>
        /// <param name="name">Attribute name.</param>
        /// <param name="value">Attribute value.</param>
        /// <returns>XmlAttribute.</returns>
        protected XmlAttribute CreateXmlAttribute(string name, string value)
        {
            XmlAttribute xmlAttribute = _xmlDocument.CreateAttribute(name);
            xmlAttribute.Value = value;

            return xmlAttribute;
        }

        /// <summary>
        /// Returns a text without special characters.
        /// </summary>
        /// <param name="text">Text to format.</param>
        /// <returns>Formatted text.</returns>
        virtual protected string GetFormattedText(string text)
        {
            Regex regex = new Regex("\\W");
            MatchCollection matches = regex.Matches(text);

            foreach (Match match in matches)
                text = text.Replace(match.Value, string.Empty);

            return text;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
I am a biomedical engineer. I work in Genoa as software developer. I developed MFC ActiveX controls for industrial automation for 2 years and packages for Visual Studio 2005 for 1 year. Currently I'm working in .NET 3.5 in biomedical area.

Comments and Discussions