Hi,
Is there a standard (framework) mapping between CLR types and xsd type codes. I need to convert a string, int, decimal etc to the equivalent XmlSchemaSimpleType.
I can construct the necessary simple type and use a case statement to do the mappings myself. I was hoping their might be a standard framework class that can either construct XmlSchemaSimpleType from the various CLR types, or perhaps even a mapping to XmlTypeCode from the CLR type.
System.String -> XmlTypeCode.String (for instance)
Thanks
UPDATE (07-07-2010) Thanks, I have read the link it needed a little tweaking - for anyone else, here is the final code that can be pasted into linqpad.
public class XmlValueWrapper
{
public object Value { get; set; }
}
public static class XsdConvert
{
private static XmlSerializer serializer = new XmlSerializer(typeof(XmlValueWrapper));
public static object ConvertFrom(string value, string xsdType)
{
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
XDocument doc = new XDocument(
new XElement("XmlValueWrapper",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(XNamespace.Xmlns + "xs", xsd),
new XElement("Value",
new XAttribute(xsi + "type", xsdType),
new XText(value))
)
);
doc.Dump("try");
using (var reader = doc.CreateReader()) {
XmlValueWrapper wrapper = (XmlValueWrapper) serializer.Deserialize(reader);
wrapper.Dump("ITEM");
return wrapper.Value;
}
}
}
public static void Main()
{
object o = XsdConvert.ConvertFrom("2010-01-02", "xs:date");
o.GetType().Dump("object");
/*
Debug.Assert(Equals(42, XsdConverta.ConvertFrom("42", "xsd:int")));
Debug.Assert(Equals(42.0, XsdConverta.ConvertFrom("42", "xsd:double")));
Debug.Assert(Equals(42m, XsdConverta.ConvertFrom("42", "xsd:decimal")));
Debug.Assert(Equals("42", XsdConverta.ConvertFrom("42", "xsd:string")));
Debug.Assert(Equals(true, XsdConverta.ConvertFrom("true", "xsd:boolean")));
Debug.Assert(Equals(new DateTime(2009, 4, 17), XsdConverta.ConvertFrom("2009-04-17", "xsd:date")));*/
}