【问题标题】:generate datacontract using svcutil.. how do I handle attributes?使用 svcutil 生成数据合同。我如何处理属性?
【发布时间】:2012-01-24 17:10:33
【问题描述】:

向我提供了一些 XSD 以及示例 xml。 一个示例 xml 的 sn-p 是:

<tfsChequeId xmlns="http://www.something.com/XMLSchemas/itrs/tfs/v1">
        <dic numericCode="20010411199194813505"/>
    </tfsChequeId>

这个sn-p的xsd如下:

<xsd:element name="tfsChequeId" type="tfs:TfsChequeId" minOccurs="1" maxOccurs="1" />

<xsd:complexType name="TfsChequeId">
<xsd:sequence>
<xsd:element name="dic" type="tfs:TfsChequeIdDic" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>The Document Identity Code (Doc-ID Code / DIC) is the representation of the document identity as printed on the document (usually also as barcode).
In many cases it also contains additional information for the form (refund rule, check digit, etc.). Several variants of the document identity code exist, but
they all contain at least the information that is included in the document identity number (country, store and serial number).
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>

    <xsd:complexType name="TfsChequeIdDic">
<xsd:attribute name="numericCode" type="common:DocumentIdentifierCode">
<xsd:annotation>
<xsd:documentation>The document identity code is the representation of the document identity as printed on the document (usually also as barcode).
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>

xsd 相当复杂。在使用 svcutil.exe 生成数据合同时,生成了许多错误。 以下所有形式:

Error: There was a validation error in the schemas provided for code generation:

Source:
Line: 85 Column: 6


Validation Error: Type 'http://www.something.com/XMLSchemas/itrs/common/v1:Docu
mentIdentifierCode' is not declared, or is not a simple type.

生成的数据合约如下:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="TfsChequeId", Namespace="http://www.something.com/XMLSchemas/itrs/tfs/v1")]
public partial class TfsChequeId : object, System.Runtime.Serialization.IExtensibleDataObject
{

    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    private www.something.com.XMLSchemas.itrs.tfs.v1.TfsChequeIdDic dicField;

    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get
        {
            return this.extensionDataField;
        }
        set
        {
            this.extensionDataField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
    public www.something.com.XMLSchemas.itrs.tfs.v1.TfsChequeIdDic dic
    {
        get
        {
            return this.dicField;
        }
        set
        {
            this.dicField = value;
        }
    }
}

但是我不知道如何使用它.. 即设置 numericCode? 任何想法/提示/提示将不胜感激。

菲奥娜

【问题讨论】:

    标签: c# wcf xsd datacontract datacontractserializer


    【解决方案1】:

    您需要做的第一件事是确保您可以运行 svcutil 而不会出错。我怀疑您的 DocumentIdentifierCode 是在未包含在命令行中的 XSD 中定义的。如果 XSD 真的很复杂,请查看this post 以了解如何重构 XSD。如果您没有那么多文件,请使用类似于this post 中所示的命令行:将 /mc 替换为 /dconly(仅用于数据协定),但请确保列出所有 XSD。我见过 svcutil 仍然无法工作的情况,在这种情况下尝试 xsd.exe 命令行(请参阅我引用的第一个链接)。

    【讨论】:

    • 谢谢佩特鲁。我现在没有错误:)
    【解决方案2】:

    属性不能与数据协定一起使用。因此,如果您必须严格遵守提供的架构,则不能使用数据协定。

    根据 XSD 的复杂性,您可以使用 svcutil 生成适合 XML 序列化的类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 2011-06-03
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      相关资源
      最近更新 更多