问题
When I use svcutil.exe to generate a Customer class from the definition contained in the xsd file:
<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>
I get the following definition of the class:
[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
{
With the Name property of DataContractAttribute having an invalid value: "Customer" (starting with uppercase letter), since, according to name property of the xs:element, it should be: "customer" (starting with lowercase letter).
I start svcutil.exe as follows:
svcutil.exe" *.xsd /t:code /dconly /n:*,Esap.AdtZapisoMessages /o:Messages.cs /tcv:Version35
The generated xml must contain a root element named "customer", and I ask you, why svcutil.exe makes this error.
回答1:
svcutil is entirely correct there; the name of the type is Customer
. From:
<xs:complexType name="Customer">
The lower-case customer
is contextual to the usage as a root element, however, that is more an xsd
/SOAP thing, and does not relate simply to the contract type in isolation. Note that svcutil is interested in contract types; it is a different tool to xsd.exe.
If you want to match a particular xml layout, svcutil is simply the wrong tool; that is a job for xsd.exe. I would expect that xsd.exe would output the required [XmlRoot("customer")]
.
I tested it, and sure enough:
[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 {
来源:https://stackoverflow.com/questions/8239630/svcutil-generates-wrong-name-property-value-in-datacontractattribute