Search code examples
.netxml-serializationwsdlxsd

Specify amount of decimal places of an xs:decimal in an XML schema


  1. Is there a way to specify the amount of decimal places an xs:decimal should have in an XML schema?

  2. Is there any way to control that using .NET's Xml*** attributes?


Solution

  • You can create a custom type that extends decimal and specify the number of digits in fractionDigits like this:

    <xs:simpleType name="twoPlacesDecimal" id="twoPlacesDecimal">
        <xs:restriction base="xs:decimal">
            <xs:fractionDigits fixed="true" value="2" />
        </xs:restriction>
    </xs:simpleType>
    

    You can specify the data type for a property using XmlAttribute(DataType = "value") but unfortunately this only supports built-in data types. From my reading of the source, if you include a custom data type you'll get an exception.