I'm wondering if there is a library - in .NET or otherwise - that will convert .NET types (eg. Integer
, String
, etc.) into XML datatypes (eg. int
, string
). Note that I'm looking to convert types, not the content of variables. For example:
var xmlType = Convert.ToXmlType(typeof(bool));
Assert.That(xmlType.ToString(), Is.EqualTo("boolean"));
I don't mind making a lookup table since I'm not dealing with too many types, but I thought it would be nice to reuse such a thing if it's already out there.
The XML data type, in fact, lies at the heart of understanding how to store and query XML data in a SQL Server database. That’s not to suggest that all XML data should be stored with the XML type, but knowing how the type works will help you determine when to use it and how to effectively access its data.
The XML Schema data type you have specified cannot be mapped to the.NET data type. The following example serializes a class named Group that contains a field named ExtraInfo, which returns an ArrayList. The example applies two instances of the XmlElementAttribute to the field and specifies different DataType values for each instance.
For the XML Schema base64Binary and hexBinary data types, use an array of Byte structures, and apply a XmlElementAttribute with the DataType set to "base64Binary" or "hexBinary", as appropriate. For the XML Schema time and date data types, use the DateTime type and apply the XmlElementAttribute with the DataType set to "date" or "time".
Gets or sets the XML Schema definition (XSD) data type of the XML element generated by the XmlSerializer. An XML Schema data type. The XML Schema data type you have specified cannot be mapped to the.NET data type.
You can use the following code to create the mappings, however it is not a one-to-one mapping. Reducing this to one-to-one will have to be an implementation detail on your end:
var mapping = (from XmlTypeCode cc in Enum.GetValues(typeof(XmlTypeCode))
let xt = XmlSchemaType.GetBuiltInSimpleType(cc)
where xt != null
group cc by xt.Datatype.ValueType into gg
select new { Type = gg.Key, XmlTypeCodes = gg.ToArray() })
.ToDictionary(m => m.Type, m => m.XmlTypeCodes);
Sample output:
System.Boolean => Boolean
System.Byte => UnsignedByte
System.Byte[] => HexBinary,Base64Binary
System.DateTime => DateTime,Time,Date,GYearMonth,GYear,GMonthDay,GDay,GMonth
System.Decimal => Decimal,Integer,NonPositiveInteger,NegativeInteger,NonNegative
Integer,PositiveInteger
...
A decent approach to solving the one-to-one problem would be to take the first entry in the code table which works for every type but String
. This also may not work for newer BCL types, but likely should going forward. It would be a breaking change for MS to rearrange the XmlTypeCode
enumeration, but that's not to say this is fool proof:
// same as above except the ToDictionary
.ToDictionary(
m => m.Type,
m => m.Type != typeof(string) ? m.XmlTypeCodes.First() : XmlTypeCode.String);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With