Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#

Simple databinding with XML - A GUI for configuration files

Rate me:
Please Sign up or sign in to vote.
4.81/5 (29 votes)
2 Feb 20049 min read 129.7K   1.3K   78  
This article is about using simple databinding on XML documents by using strongly typed XML documents generated at runtime, taking advantage of System.Reflection.Emit and MSIL.
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms;
using System.Reflection;

namespace XmlBindingManager
{
	public class TagSettingTypeConverter : ExpandableObjectConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			if(sourceType == typeof(string)) return true;
			return base.CanConvertFrom (context, sourceType);
		}

		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if(value is string) 
			{
				try 
				{
					string propertyList = (string) value;
					string[] properties = propertyList.Split(';');
					TagSetting setting = new TagSetting();
					setting.XmlTag = properties[0].Trim();
					setting.KeyAttribute = properties[1].Trim();
					return setting;
				}
				catch {}
				throw new ArgumentException("arguments not valid");
			}

			return base.ConvertFrom (context, culture, value);
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			if(destinationType == typeof(InstanceDescriptor)) return true;
			return base.CanConvertTo (context, destinationType);
		}

		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			if(value is TagSetting) 
			{
				TagSetting setting = value as TagSetting;
				if(destinationType == typeof(string)) 
				{
					return string.Format("{0}; {1}", setting.XmlTag, setting.KeyAttribute);
				}
				if(destinationType == typeof(InstanceDescriptor)) 
				{
					object[] properties = new object[2] {setting.XmlTag, setting.KeyAttribute};
					Type[] types = new Type[2] {typeof(string), typeof(string)};

					ConstructorInfo ci = typeof(TagSetting).GetConstructor(types);
					return new InstanceDescriptor(ci, properties);
				}
			}

			return base.ConvertTo (context, culture, value, destinationType);
		}

		public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
		{
			return true;
		}

		public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
		{
			TagSetting setting = new TagSetting();
			setting.XmlTag = (string) propertyValues["XmlTag"];
			setting.KeyAttribute = (string) propertyValues["KeyAttribute"];
			return setting;
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions