0
votes

I'm using xml2code library to convert xsd file to C# code.

In Generator.xs file, the following code is mapping the xsd types to C# types:

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

All types are mapped pretty good, except positiveInteger (from the namespace "http://www.w3.org/2001/XMLSchema").

I've tried to fix it from code, somthing like:

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

but unfortunately all XmlTypeMapping properties are read only, so I can't event create a new instance to send to the function exporter.ExportTypeMapping().

I have no idea how to change the mapped type.

1

1 Answers

0
votes

It seems that Microsoft XmlSchemaImportersd library (inside System.Xml.Serialization), is mapping the following types to String:

negativeInteger
nonNegativeInteger
nonPositiveInteger
positiveInteger

So to force XmlSchemaImportersd to map into a C# base types you need to use the following xs: types instaed:

byte
decimal
int
integer
long
short
unsignedLong
unsignedInt
unsignedShort
unsignedByte