为什么xsd。exe为xs:integer生成字符串属性?

时间:2022-10-02 17:18:23

When I generate a c# class from a xsd schema with xsd.exe I find this behaivor a bit wierd.

当我使用xsd从xsd模式生成c#类时。我觉得这种做法有点过分。

My element:

我的元素:

<xs:element name="InvoiceNo" type="xs:integer"/>

is generated to:

生成:

[System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order=5)]
public string InvoiceNo
{
   ...
}

Why is that property not generated as an int instead of string?

为什么这个属性不是作为int而不是字符串生成的呢?

1 个解决方案

#1


54  

This behavior is by design:

这种行为是故意的:

The xs:integer type is specified as a number with no upper or lower bound on its size. For this reason, neither XML serialization nor validation map it to the System.Int32 type. Instead, XML serialization maps the xs:integer to a string while validation maps it to the Decimal type that is much larger than any of the integer types in the .NET Framework

整数类型被指定为一个在其大小上没有上限或下限的数字。因此,无论是XML序列化还是验证都不会将其映射到系统。Int32类型。相反,XML序列化将xs:integer映射到字符串,而验证将它映射到比.NET框架中的任何整数类型都大得多的Decimal类型

Use xs:int, which is a signed 32-bit integer, to have Xsd.exe map it to a System.Int32:

使用带有符号的32位整数xs:int具有Xsd。exe将其映射到系统。

<xs:element name="InvoiceNo" type="xs:int" />

Here's a detailed list of the data types defined in the XML Schema Definition standard.

下面是XML模式定义标准中定义的数据类型的详细列表。

#1


54  

This behavior is by design:

这种行为是故意的:

The xs:integer type is specified as a number with no upper or lower bound on its size. For this reason, neither XML serialization nor validation map it to the System.Int32 type. Instead, XML serialization maps the xs:integer to a string while validation maps it to the Decimal type that is much larger than any of the integer types in the .NET Framework

整数类型被指定为一个在其大小上没有上限或下限的数字。因此,无论是XML序列化还是验证都不会将其映射到系统。Int32类型。相反,XML序列化将xs:integer映射到字符串,而验证将它映射到比.NET框架中的任何整数类型都大得多的Decimal类型

Use xs:int, which is a signed 32-bit integer, to have Xsd.exe map it to a System.Int32:

使用带有符号的32位整数xs:int具有Xsd。exe将其映射到系统。

<xs:element name="InvoiceNo" type="xs:int" />

Here's a detailed list of the data types defined in the XML Schema Definition standard.

下面是XML模式定义标准中定义的数据类型的详细列表。