xsd.exe 不会为 xs:list 标记创建集合(数组或列表)

Mon*_*nku 5 xml xsd xsd.exe

我有以下 xsd 标签,我希望在反序列化时创建一个集合,但它没有。

<Metrics>
....
....
<xs:simpleType name="idType">
    <xs:restriction base="xs:int">
        <xs:minInclusive value="0"/>
        <xs:maxInclusive value="99"/>
    </xs:restriction>
</xs:simpleType>
...
...
<xs:element name="RPT">
    <xs:simpleType>
        <xs:list itemType="idType"/>
    </xs:simpleType>
</xs:element>
...
...
</Metrics>
Run Code Online (Sandbox Code Playgroud)

当我使用 command 创建类时xsd /c <xsd-filename>.xsd,创建的 RPT 是:

public partial class Metrics 
{
   ...
   ...
   private string rPTField;
   ...
   ...
}
Run Code Online (Sandbox Code Playgroud)

我想RPT成为一个返回列表元素的集合类型。我怎样才能做到这一点 ?

Spr*_*tty 0

基本上,Xsd.exe 倾向于将嵌套类型视为字符串,因为它们可能变得非常复杂,受限制类型的联合列表等......如果您有 xs:int 列表,您可能有机会。

<xs:element name="RPT">
    <xs:simpleType>
        <xs:list itemType="xs:int"/>
    </xs:simpleType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)

或者查看Xml Objects 在线生成器,它会从您的 XSD 生成如下所示的代码

XSD

<!--Created with Liquid Studio 2019 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="idType">
        <xs:restriction base="xs:int">
            <xs:minInclusive value="0" />
            <xs:maxInclusive value="99" />
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="MyRootElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="RPT">
                    <xs:simpleType>
                        <xs:list itemType="idType" />
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

生成的代码

#region Elements
/// <summary>A class representing the root XSD element MyRootElement</summary>
/// <XsdPath>schema:schema.xsd/element:MyRootElement</XsdPath>
/// <XsdFile>file://sandbox/schema.xsd</XsdFile>
/// <XsdLocation>10:5-20:18</XsdLocation>
[LxSimpleElementDefinition("MyRootElement", "", ElementScopeType.GlobalElement)]
public partial class MyRootElementElm
{
    /// <summary>A <see cref="System.Int32" />[], Required : should not be set to null</summary>
    [LxElementValue("RPT", "", LxValueType.List, XsdType.XsdInt, MinOccurs = 1, MaxOccurs = 1)]
    public System.Int32[] RPT { get; set; } = new System.Int32[] {};

}
#endregion
Run Code Online (Sandbox Code Playgroud)

它不会拾取嵌套的构面 0-99,但它会获得正确的类型。对于小型 XSD 也是免费的。