Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

An XML Schema Definition (XSD) Editor and Strongly-Typed Configuration Class Generator

, 20 Jul 2009
An editor capable of producing commmon XSD documents and generating strongly-typed configuration class corresponding to the XML schema (XSD) file
ConfigClassGen_demo.zip
ConfigClassGen_demo
VS2005
ConfigClassGenDemo
ConfigClassGenDemo
bin
Debug
ConfigClassGen.exe
ConfigClassGenDemo.csproj.user
Properties
VS2008
ConfigClassGenDemo
ConfigClassGenDemo
bin
Debug
ConfigClassGen.exe
ConfigClassGenDemo.csproj.user
Properties
ConfigClassGen_exe.zip
ConfigClassGen_exe
DotNet2.0
ConfigClassGen
ConfigClassGen.exe
ConfigClassGenDemo
ConfigClassGenDemo.exe
DotNet3.5
ConfigClassGen
ConfigClassGen.exe
ConfigClassGenDemo
ConfigClassGenDemo.exe
ConfigClassGen_src.zip
ConfigClassGen_src
VS2005
ConfigClassGen
ConfigClassGen
Properties
Settings.settings
VS2008
ConfigClassGen
ConfigClassGen
Properties
Settings.settings
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.3082
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace ConfigClassGenDemo
{
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Xml;
    using System.IO;
    using System.Web;
    
    
    internal class AppConfigHandler : IConfigurationSectionHandler
    {
        
        public object Create(object parent, object context, XmlNode node)
        {

			try
			{
				AppConfig _config = new AppConfig();
				_config.LoadValues(node);

				return _config;
			}
			catch(Exception ex)
			{
				throw ex;
			}

        }
    }
    
    public sealed class AppConfig
    {
        
        private DEV fldDEV;
        
        private int fldClientID;
        
        private string fldClientName;
        
        private ProvinceCollection fldProvinceCollection = new ProvinceCollection();
        
        private ContactCollection fldContactCollection = new ContactCollection();
        
        public DEV DEVField
        {
            get
            {
                return this.fldDEV;
            }
            set
            {
            }
        }
        
        public int ClientID
        {
            get
            {
                return this.fldClientID;
            }
        }
        
        public string ClientName
        {
            get
            {
                return this.fldClientName;
            }
        }
        
        public ProvinceCollection ProvinceCollections
        {
            get
            {
                return this.fldProvinceCollection;
            }
        }
        
        public ContactCollection ContactCollections
        {
            get
            {
                return this.fldContactCollection;
            }
        }
        
        public static AppConfig GetConfig(string sectionName)
        {

		try
		{
			return (AppConfig)ConfigurationSettings.GetConfig(sectionName);
		}
		catch(Exception ex)
		{
			throw ex;
		}

        }
        
        internal void LoadValues(XmlNode node)
        {

		XmlNode currentConfigNode = null;

		try
		{
			XmlNode appEnvironmentNode = node.SelectSingleNode("AppEnvironment");
			if (appEnvironmentNode != null)
			{
				XmlAttribute att = appEnvironmentNode.Attributes["configSource"];
				if (att == null)
					throw new Exception("configSource attribute missed.");
				if ((att.Value == null) || (att.Value.Trim().Length == 0))
					throw new Exception("configSource attribute value missed");
				string environmentConfig = att.Value;

				// to get the path for the executable file that started the application, without the executable name
				string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

				string xmlFileName = Path.Combine(appPath, environmentConfig);

				if (File.Exists(xmlFileName))
				{
					XmlDocument doc = new XmlDocument();

					doc.Load(xmlFileName);

					if (doc.HasChildNodes)
					{
						XmlNode childNode = doc.SelectSingleNode("AppConfigSettings");
						if (childNode != null)
						{
							LoadAppValues(childNode);
						}
					}
					else
					{
						throw new Exception("Bad file format in config source file: " + xmlFileName);
					}

				}
				else
				{
					throw new Exception("Cannot find sustom config file: " + xmlFileName);
				}
			}
			else
				throw new Exception("AppEnvironment must be specified.");
		}
		catch (Exception ex)
		{
			throw new Exception("Configuration error in AppConfig section: " + ex.Message);
		}	

        }
        
        protected void LoadAppValues(XmlNode currentConfigNode)
        {
            XmlNode node;
            XmlNodeList nodeList;

            node = currentConfigNode.SelectSingleNode("ClientID");
            if (((node != null) 
                        && (node.InnerText.Trim() != string.Empty)))
            {
                try
                {
                    this.fldClientID = int.Parse(node.InnerText);
                }
                catch (System.Exception )
                {
                    throw new Exception("ClientID must be an integer");
                }
            }

            node = currentConfigNode.SelectSingleNode("ClientName");
            if (((node != null) 
                        && (node.InnerText.Trim() != string.Empty)))
            {
                this.fldClientName = node.InnerText;
            }

            nodeList = currentConfigNode.SelectNodes("Province");
            if ((nodeList != null))
            {
                // 
                IEnumerator eProvince = nodeList.GetEnumerator();
                for (
                ; eProvince.MoveNext(); 
                )
                {
                    XmlNode nProvince = ((XmlNode)(eProvince.Current));


                    string l_fldProvince_Abbreviation = string.Empty;
                    node = nProvince.SelectSingleNode("Abbreviation");
                    if (((node != null) 
                                && (node.InnerText.Trim() != string.Empty)))
                    {
                        l_fldProvince_Abbreviation = node.InnerText;
                    }

                    string l_fldProvince_Name = string.Empty;
                    node = nProvince.SelectSingleNode("Name");
                    if (((node != null) 
                                && (node.InnerText.Trim() != string.Empty)))
                    {
                        l_fldProvince_Name = node.InnerText;
                    }

                    long l_fldProvince_Population = long.MinValue;
                    node = nProvince.SelectSingleNode("Population");
                    if (((node != null) 
                                && (node.InnerText.Trim() != string.Empty)))
                    {
                        try
                        {
                            l_fldProvince_Population = long.Parse(node.InnerText);
                        }
                        catch (System.Exception )
                        {
                            throw new Exception("Population must be a long");
                        }
                    }
                    ProvinceCollection.Province l_Province = new ProvinceCollection.Province(l_fldProvince_Abbreviation, l_fldProvince_Name, l_fldProvince_Population);

                    // add this Province object to the collection
                    this.fldProvinceCollection.Add(l_Province);
                }
            }

            nodeList = currentConfigNode.SelectNodes("Contact");
            if ((nodeList != null))
            {
                // 
                IEnumerator eContact = nodeList.GetEnumerator();
                for (
                ; eContact.MoveNext(); 
                )
                {
                    XmlNode nContact = ((XmlNode)(eContact.Current));


                    string l_fldContact_Name = string.Empty;
                    node = nContact.SelectSingleNode("Name");
                    if (((node != null) 
                                && (node.InnerText.Trim() != string.Empty)))
                    {
                        l_fldContact_Name = node.InnerText;
                    }

                    string[] l_fldEmailAddress = null;
                    XmlNodeList nodeListEmailAddress = nContact.SelectNodes("EmailAddress");
                    if ((nodeListEmailAddress != null))
                    {
                        l_fldEmailAddress = new string[nodeListEmailAddress.Count];
                        int i = 0;
                        IEnumerator eEmailAddress = nodeListEmailAddress.GetEnumerator();
                        for (
                        ; eEmailAddress.MoveNext(); 
                        )
                        {
                            XmlNode nEmailAddress = ((XmlNode)(eEmailAddress.Current));

                            l_fldEmailAddress[i++] = nEmailAddress.InnerText;
                        }
                    }

                    XmlNode nAddress = nContact.SelectSingleNode("Address");

                    string l_fldAddress_StreetAddress = string.Empty;
                    node = nAddress.SelectSingleNode("StreetAddress");
                    if (((node != null) 
                                && (node.InnerText.Trim() != string.Empty)))
                    {
                        l_fldAddress_StreetAddress = node.InnerText;
                    }

                    string l_fldAddress_City = string.Empty;
                    node = nAddress.SelectSingleNode("City");
                    if (((node != null) 
                                && (node.InnerText.Trim() != string.Empty)))
                    {
                        l_fldAddress_City = node.InnerText;
                    }

                    string l_fldAddress_Province = string.Empty;
                    node = nAddress.SelectSingleNode("Province");
                    if (((node != null) 
                                && (node.InnerText.Trim() != string.Empty)))
                    {
                        l_fldAddress_Province = node.InnerText;
                    }
                    ContactCollection.Contact.Address l_Address = new ContactCollection.Contact.Address(l_fldAddress_StreetAddress, l_fldAddress_City, l_fldAddress_Province);
                    ContactCollection.Contact l_Contact = new ContactCollection.Contact(l_fldContact_Name, l_fldEmailAddress, l_Address);

                    // add this Contact object to the collection
                    this.fldContactCollection.Add(l_Contact);
                }
            }
        }
        
        public sealed class DEV
        {
        }
        
        public sealed class ProvinceCollection : CollectionBase
        {
            
            public Province this[int index]
            {
                get
                {
                    return ((Province)(this.List[index]));
                }
                set
                {
                    this.List[index] = value;
                }
            }
            
            public void Add(Province _Province)
            {
this.List.Add(_Province);
            }
            
            public int GetIndexByAbbreviation(string _Abbreviation)
            {

		for (int i=0; i < List.Count; i++)
		{
			if ( ((Province)List[i]).Abbreviation == _Abbreviation)
			{
				return i;
			}
		}

		throw new Exception("Cannot identify Abbreviation: " + _Abbreviation);
                
            }
            
            public int GetIndexByName(string _Name)
            {

		for (int i=0; i < List.Count; i++)
		{
			if ( ((Province)List[i]).Name == _Name)
			{
				return i;
			}
		}

		throw new Exception("Cannot identify Name: " + _Name);
                
            }
            
            public int GetIndexByPopulation(long _Population)
            {

		for (int i=0; i < List.Count; i++)
		{
			if ( ((Province)List[i]).Population == _Population)
			{
				return i;
			}
		}

		throw new Exception("Cannot identify Population: " + _Population);
                
            }
            
            public sealed class Province
            {
                
                private string fldAbbreviation;
                
                private string fldName;
                
                private long fldPopulation;
                
                public Province(string _fldAbbreviation, string _fldName, long _fldPopulation)
                {
                    fldAbbreviation = _fldAbbreviation;
                    fldName = _fldName;
                    fldPopulation = _fldPopulation;
                }
                
                public string Abbreviation
                {
                    get
                    {
                        return this.fldAbbreviation;
                    }
                }
                
                public string Name
                {
                    get
                    {
                        return this.fldName;
                    }
                }
                
                public long Population
                {
                    get
                    {
                        return this.fldPopulation;
                    }
                }
            }
        }
        
        public sealed class ContactCollection : CollectionBase
        {
            
            public Contact this[int index]
            {
                get
                {
                    return ((Contact)(this.List[index]));
                }
                set
                {
                    this.List[index] = value;
                }
            }
            
            public void Add(Contact _Contact)
            {
this.List.Add(_Contact);
            }
            
            public int GetIndexByName(string _Name)
            {

		for (int i=0; i < List.Count; i++)
		{
			if ( ((Contact)List[i]).Name == _Name)
			{
				return i;
			}
		}

		throw new Exception("Cannot identify Name: " + _Name);
                
            }
            
            public int GetIndexByEmailAddress(string[] _EmailAddress)
            {

		for (int i=0; i < List.Count; i++)
		{
			if ( ((Contact)List[i]).EmailAddress == _EmailAddress)
			{
				return i;
			}
		}

		throw new Exception("Cannot identify EmailAddress: " + _EmailAddress);
                
            }
            
            public sealed class Contact
            {
                
                private string fldName;
                
                private string[] fldEmailAddress;
                
                private Address fldAddress;
                
                public Contact(string _fldName, string[] _fldEmailAddress, Address _fldAddress)
                {
                    fldName = _fldName;
                    fldEmailAddress = _fldEmailAddress;
                    fldAddress = _fldAddress;
                }
                
                public string Name
                {
                    get
                    {
                        return this.fldName;
                    }
                }
                
                public string[] EmailAddress
                {
                    get
                    {
                        return this.fldEmailAddress;
                    }
                }
                
                public Address AddressField
                {
                    get
                    {
                        return this.fldAddress;
                    }
                    set
                    {
                    }
                }
                
                public sealed class Address
                {
                    
                    private string fldStreetAddress;
                    
                    private string fldCity;
                    
                    private string fldProvince;
                    
                    public Address(string _fldStreetAddress, string _fldCity, string _fldProvince)
                    {
                        fldStreetAddress = _fldStreetAddress;
                        fldCity = _fldCity;
                        fldProvince = _fldProvince;
                    }
                    
                    public string StreetAddress
                    {
                        get
                        {
                            return this.fldStreetAddress;
                        }
                    }
                    
                    public string City
                    {
                        get
                        {
                            return this.fldCity;
                        }
                    }
                    
                    public string Province
                    {
                        get
                        {
                            return this.fldProvince;
                        }
                    }
                }
            }
        }
    }
}

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)

Share

About the Author

Song_Gao
Software Developer (Senior) ThinData
Canada Canada
Song is a senior .NET Developer specialized on both Web and Windows applications. He is an MCTS (Microsoft Certified Technology Specialist) for Windows Applications, Web Applications and Distributed Applications.
 
He currently lives in Toronto and likes to travel.
 
You may contact Song through his email: song_gao@hotmail.com

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 Jul 2009
Article Copyright 2009 by Song_Gao
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid