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:
我想知道是否有一个库——在。net中或者其他地方——可以转换。net类型。将整数、字符串等)转换成XML数据类型(如。整数、字符串)。注意,我希望转换类型,而不是变量的内容。例如:
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.
我不介意做一个查找表,因为我没有处理太多类型的东西,但是我想如果它已经存在了,重用这样的东西会很好。
2 个解决方案
#1
3
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:
解决一对一问题的一个不错的方法是在代码表中使用第一个条目,该条目适用于除字符串之外的所有类型。这对于新的BCL类型也可能不起作用,但可能会继续。对MS来说,重新排列XmlTypeCode枚举是一个重大的改变,但这并不是说这是一个愚蠢的证明:
// same as above except the ToDictionary
.ToDictionary(
m => m.Type,
m => m.Type != typeof(string) ? m.XmlTypeCodes.First() : XmlTypeCode.String);
#2
2
I found a second aproach using the framework itselfe.
我找到了第二个使用框架的方法。
XmlTypeMapping getQualifiedNameForSystemType(Type systemType_in)
{
SoapReflectionImporter sri = new SoapReflectionImporter();
return sri.ImportTypeMapping(systemType_in);
}
#1
3
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:
解决一对一问题的一个不错的方法是在代码表中使用第一个条目,该条目适用于除字符串之外的所有类型。这对于新的BCL类型也可能不起作用,但可能会继续。对MS来说,重新排列XmlTypeCode枚举是一个重大的改变,但这并不是说这是一个愚蠢的证明:
// same as above except the ToDictionary
.ToDictionary(
m => m.Type,
m => m.Type != typeof(string) ? m.XmlTypeCodes.First() : XmlTypeCode.String);
#2
2
I found a second aproach using the framework itselfe.
我找到了第二个使用框架的方法。
XmlTypeMapping getQualifiedNameForSystemType(Type systemType_in)
{
SoapReflectionImporter sri = new SoapReflectionImporter();
return sri.ImportTypeMapping(systemType_in);
}