Get XML data type from .NET type?

I am wondering if there is a library - in .NET or otherwise - that converts .NET types (e.g.. Integer, StringEtc.) into XML data types (e.g. int, String). Note that I'm looking to convert types, not the contents of variables. For instance:

var xmlType = Convert.ToXmlType(typeof(bool));
Assert.That(xmlType.ToString(), Is.EqualTo("boolean"));

I am not opposed to creating a lookup table since I am not dealing with too many types, but I thought it would be nice to repeat such a thing if it is already there.

+5
source share
2 answers

, " ". -- :

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);

:

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
...

"--" , , String. BCL, , , . MS, XmlTypeCode, , :

// same as above except the ToDictionary
.ToDictionary(
    m => m.Type,
    m => m.Type != typeof(string) ? m.XmlTypeCodes.First() : XmlTypeCode.String);
+3

aproach, .

XmlTypeMapping getQualifiedNameForSystemType(Type systemType_in)
{
    SoapReflectionImporter sri = new SoapReflectionImporter();
    return sri.ImportTypeMapping(systemType_in);
}
+2

All Articles