Initially implemented as XmlFragment converter but broke it down to a kludged document converter when I ran into the save problem.
Both implementations load and present the entity property as xml object but neither call ConvertTo to get the string to stick back in the SqlDbType.Xml column.
Any ideas? I am sure I am just having a brain fart.
public class XmlDocumentStringConverter:TypeConverter
{
///<summary>
///</summary>
public XmlDocumentStringConverter()
{
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
switch(sourceType.FullName)
{
case "System.String":
return true;
default:
return false;
}
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
switch(destinationType.FullName)
{
case "System.String":
return true;
default:
return false;
}
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
XmlDocument toReturn;
switch(value.GetType().FullName)
{
case "System.DBNull":
return null;
break;
case "System.String":
var doc = new XmlDocument();
//// need to wrap as xml column can contain fragment.
doc.LoadXml("<wrapper>" + value + "</wrapper>");
toReturn = doc;
break;
default:
throw new NotSupportedException("Conversion from a value of type '" + value.GetType() + "' to System.String isn't supported");
}
return toReturn;
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if(value==null)
{
return System.DBNull.Value;
}
if (!(value is XmlDocument))
{
throw new ArgumentException("Value isn't of type XmlDocument", "value");
}
return ((XmlDocument)value).OuterXml;
}
public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
var doc = new XmlDocument();
return doc;
}
}