2011-11-23 63 views
2

产生错误的名称属性值当我使用svcutil.exe的生成自定义包含在XSD文件一个Customer类:SvcUtil工具在DataContractAttribute

<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:元素的name属性,它应该是:“customer”(以小写字母开头) 。

我开始svcutil.exe的如下:

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

生成的XML必须包含一个名为“客户”的根元素,我问你,为什么svcutil.exe的使这个错误。

回答

4

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 {