.net 如何使数组具有除“以外的名称?”;项目“;从XSD模式生成C#代码时?

.net 如何使数组具有除“以外的名称?”;项目“;从XSD模式生成C#代码时?,.net,web-services,xsd,attributes,code-generation,.net,Web Services,Xsd,Attributes,Code Generation,我正在从事一个项目,该项目必须连接到一些古老的Web服务,这些Web服务将请求和响应的层次数据打包成单个层次XML字符串 我一直在使用xsd.exe从示例请求和响应XML片段生成xsd,在必要时对其进行修改,使其成为最佳定义,并再次使用xsd.exe生成C#对象。然后,调用Web服务的管理器可以将这些强类型请求对象作为参数,将它们序列化为字符串以进行调用,将响应作为字符串返回,将它们反序列化为强类型响应对象并返回这些响应 比如说,如果我有一个字符串列表,我可以有一个有效的XSD,它认为它是一个复

我正在从事一个项目,该项目必须连接到一些古老的Web服务,这些Web服务将请求和响应的层次数据打包成单个层次XML字符串

我一直在使用xsd.exe从示例请求和响应XML片段生成xsd,在必要时对其进行修改,使其成为最佳定义,并再次使用xsd.exe生成C#对象。然后,调用Web服务的管理器可以将这些强类型请求对象作为参数,将它们序列化为字符串以进行调用,将响应作为字符串返回,将它们反序列化为强类型响应对象并返回这些响应

比如说,如果我有一个字符串列表,我可以有一个有效的XSD,它认为它是一个复杂类型的无界xs:choice of xs:string元素,然后它将简单地反序列化为一个字符串数组,这很好,也很容易处理。恼人的问题是,出于某种原因,似乎没有任何方法可以让它调用字符串数组,而不是“Items”。无论我在模式中添加了什么,我都无法让xsd.exe写入任何其他名称

下面是一个XSD模式示例:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="AccountStatusRequest" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="AccountStatusRequest">
        <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded" id="AccountRowIDs">
                <xs:element nillable="true" type="xs:string" id="AccountRowID" name="AccountRowID"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>
</xs:schema>
我已经尝试将几乎所有的msdata:intellisense属性都添加到choice和内部元素中,但没有任何区别

为了便于讨论,我在complexType中添加了两个额外的类似选项,以查看这是否会迫使它为字符串数组使用名称,但它给了我Items、Items1、Items2

我真的不希望它必须是一个只包含字符串的自己类型的数组,但我也真的不希望它被称为“Items”,没有xml注释(有人知道如何将它们添加到xsd中吗?),因为它应该有一个更具描述性的名称。我绝对不能在Web服务模式发生更改时手动将其更改为所需的工作流,而是更改XSD,然后从中重新生成类


这似乎是xsd.exe应该支持的另一个功能。有什么我遗漏的吗?我应该采取不同的方法吗?或者有没有一个替代的工具,我可以用它来代替,它不那么蹩脚?

我很确定这是xsd.exe的一个限制;您可以从另一个方向(class=>xml/xsd)获得自定义结果,但这对您帮助不大。抱歉。

尝试将选项更改为序列,并将最小值/最大值出现在AccountRowID元素中…

编辑:抱歉,我不知怎么错过了前面的答案-它对我有用-这是我使用的模式和结果。我最初的答案也可能是合适的

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="AccountStatusRequest" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="AccountStatusRequest" msdata:ColumnName="AccountList">
        <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="unbounded" id="AccountRowIDs">
              <xs:element nillable="true" type="xs:string" id="AccountRowID" name="AccountRowID"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>  
</xs:schema>

public partial class AccountStatusRequest {

    private string[] accountRowIDField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("AccountRowID", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public string[] AccountRowID {
        get {
            return this.accountRowIDField;
        }
        set {
            this.accountRowIDField = value;
        }
    }
}

公共部分类AccountStatusRequest{
私有字符串[]accountRowIDField;
/// 
[System.Xml.Serialization.xmlementAttribute(“AccountRowID”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=true)]
公共字符串[]AccountRowID{
得到{
返回此.accountRowIDField;
}
设置{
this.accountRowIDField=值;
}
}
}
您好,在这里,我稍微更改了模式,用一个由simpleType和一个属性包装的列表替换了选项,该列表生成了所需的输出(命名字符串数组)。但是,我不确定修改后的模式是否适合您所做的工作

以下是修改后的模式:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="AccountStatusRequest" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="AccountStatusRequest" msdata:ColumnName="AccountList">
        <xs:complexType>
          <xs:attribute name="AccountRowIDs">
            <xs:simpleType id="AccountRowID"  >
              <xs:list itemType="xs:string">                
              </xs:list>
            </xs:simpleType>       
          </xs:attribute>
        </xs:complexType>
    </xs:element>  
</xs:schema>

..以及使用xsd.exe运行后的输出:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class AccountStatusRequest {

    private string[] accountRowIDsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string[] AccountRowIDs {
        get {
            return this.accountRowIDsField;
        }
        set {
            this.accountRowIDsField = value;
        }
    }
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”、“2.0.50727.3038”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类AccountStatusRequest{
私有字符串[]accountRowIDsField;
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串[]AccountRowIDs{
得到{
返回此.accountRowIDsField;
}
设置{
this.accountRowIDsField=值;
}
}
}

很抱歉,我不确定我怎么会错过这个答案-它对我有效+1
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class AccountStatusRequest {

    private string[] accountRowIDsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string[] AccountRowIDs {
        get {
            return this.accountRowIDsField;
        }
        set {
            this.accountRowIDsField = value;
        }
    }
}