2

我们有一个大型 wcf 服务和一个大型 xsd 文档,其中包含多个元素和复杂类型。我们使用 xsd.exe 生成代码,并使用XmlSerializerFormat我们ServiceContract的 WCF 服务再次序列化这些对象。

现在我们遇到了 xsd.exe 和字符串数组定义的问题。

图像我们定义了以下元素..

<xs:element name="Configuration" type="Configuration"/>
<xs:complexType name="Configuration">
    <xs:sequence>
    <xs:element name="Name" type="xs:string"/>
    <xs:element name="Parameters" type="Parameters" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="Parameters">
     <xs:sequence>
    <xs:element name="Parameter" type="xs:string" maxOccurs="unbounded"/>
      </xs:sequence>
  </xs:complexType>

这将导致:

 /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://mynamespace.com/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://mynamespace.com/", IsNullable=false)]
    public partial class Configuration {

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string Name;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        [System.Xml.Serialization.XmlArrayItemAttribute("Parameter", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
        public string[] Parameters;
    }

如您所见,xsd.exe 工具足够智能,可以查看 complextype Parameters,因此将其设为string[].

问题是,如果我们在 WCF 服务中使用它,它将创建ArrayOfString复杂类型而不是我们的Parameters复杂类型。更糟糕的是,我们有几个 string[] 导致ArrayOfString1,ArrayOfString2等等ArrayOfString3..

问题是:我们如何避免 XSD 使我们的复杂类型变平Parameters

4

0 回答 0