Finding CLR Types from Schema
How can I found out what CLR type a particular XML schema definition will map to when using data contracts?
Ask the type system what type it thinks the schema will map to.
static string ClrTypeForXmlType(XmlQualifiedName xmlType)
{
return new XsdDataContractImporter().GetCodeTypeReference(xmlType).BaseType;
}
static void Main(string[] args)
{
Console.WriteLine(ClrTypeForXmlType(new XmlQualifiedName("duration", "www.w3.org/2001/XMLSchema")));
Console.WriteLine(ClrTypeForXmlType(new XmlQualifiedName("token", "www.w3.org/2001/XMLSchema")));
Console.WriteLine(ClrTypeForXmlType(new XmlQualifiedName("dateTime", "www.w3.org/2001/XMLSchema")));
Console.WriteLine(ClrTypeForXmlType(new XmlQualifiedName("anyURI", "www.w3.org/2001/XMLSchema")));
Console.WriteLine(ClrTypeForXmlType(new XmlQualifiedName("decimal", "www.w3.org/2001/XMLSchema")));
Console.WriteLine(ClrTypeForXmlType(new XmlQualifiedName("positiveInteger", "www.w3.org/2001/XMLSchema")));
Console.WriteLine(ClrTypeForXmlType(new XmlQualifiedName("QName", "www.w3.org/2001/XMLSchema")));
}
This produces the following list of types.
System.TimeSpan
System.String
System.DateTime
System.Uri
System.Decimal
System.Int64
System.Xml.XmlQualifiedName