【问题标题】:Svcutil generates wrong Name property value in DataContractAttributeSvcutil 在 DataContractAttribute 中生成错误的 Name 属性值
【发布时间】:2012-01-04 14:15:21
【问题描述】:

当我使用 svcutil.exe 从 xsd 文件中包含的定义生成 Customer 类时:

<xs:schema ...>

<xs:element name="customer" type="Customer" nillable="true" />

<xs:complexType name="Customer">
  <xs:sequence>
    <xs:element name="id" type="xs:decimal" minOccurs="0" />
    <xs:element name="first_name" type="xs:string" />
    <xs:element name="last_name" type="xs:string" />
    <xs:element name="phone" type="Phone" minOccurs="0" />
    <xs:element name="email" type="Email" minOccurs="0" />
    <xs:element name="personal_id" type="xs:string" minOccurs="0" />
    <xs:element name="address" type="Address" minOccurs="0" />
    <xs:element name="status" type="CustomerStatus" />
  </xs:sequence>
</xs:complexType>

</xs:schema>

我得到以下类的定义:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="Customer", Namespace="http://www.bluewhite.pl/api/1.0")]
public partial class Customer : object, System.Runtime.Serialization.IExtensibleDataObject
{

DataContractAttribute 的 Name 属性值无效:“Customer”(以大写字母开头),因为根据 xs:element 的 name 属性,它应该是:“customer”(以小写字母开头)。

我启动svcutil.exe如下:

svcutil.exe" *.xsd /t:code /dconly /n:*,Esap.AdtZapisoMessages /o:Messages.cs /tcv:Version35

生成的xml必须包含一个名为“customer”的根元素,我问你,为什么svcutil.exe会出现这个错误。

【问题讨论】:

    标签: c# xsd datacontractserializer wcf-rest svcutil.exe


    【解决方案1】:

    svcutil 在那里是完全正确的; 类型的名称是Customer。来自:

    <xs:complexType name="Customer">
    

    小写的customer 与用作根元素的上下文相关,但是,它更像是xsd/SOAP 事物,并且不仅仅与孤立的合同类型相关。注意 svcutil 对合约类型感兴趣;它是与 xsd.exe 不同的工具。

    如果你想匹配一个特定的 xml 布局,svcutil 是一个错误的工具;这是 xsd.exe 的工作。我希望 xsd.exe 会输出所需的[XmlRoot("customer")]

    我测试过了,果然:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlRootAttribute("customer", Namespace="",
              IsNullable=true)]
    public partial class Customer {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多