xml2code lib从`positiveInteger`生成`string`类型

时间:2014-11-02 07:39:23

标签: c# xsd

我正在使用xml2code库将xsd文件转换为C#代码。

Generator.xs文件中,以下代码将xsd类型映射到C#类型:

var importer = new XmlSchemaImporter(schemas, generationOptions, new ImportContext(new CodeIdentifiers(), false));

foreach (XmlSchemaType type in xsd.Items.OfType<XmlSchemaType>())
{
    var mapping = importer.ImportSchemaType(type.QualifiedName);
    exporter.ExportTypeMapping(mapping);
}

除了positiveInteger(来自命名空间“http://www.w3.org/2001/XMLSchema”)之外,所有类型都映射得非常好。

我试图通过代码解决它,例如:

switch (mapping.XsdTypeName)
{
    case "positiveInteger":
        mapping.TypeFullName = "System.UInt32"; // was "System.String"
        mapping.TypeName = "UInt32";            // was "String"
        break;
    default:
        break;
}

但遗憾的是,所有XmlTypeMapping属性都是只读的,因此我无法创建要发送到函数exporter.ExportTypeMapping()的新实例。

我不知道如何更改映射类型。

1 个答案:

答案 0 :(得分:0)

似乎Microsoft XmlSchemaImportersd库(在System.Xml.Serialization内)将以下类型映射到String

negativeInteger
nonNegativeInteger
nonPositiveInteger
positiveInteger

因此要强制XmlSchemaImportersd映射到C#基类型,您需要使用以下xs:类型的instaed:

byte
decimal
int
integer
long
short
unsignedLong
unsignedInt
unsignedShort
unsignedByte