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.
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:
Sample output:
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 theXmlTypeCodeenumeration, but that’s not to say this is fool proof: