【发布时间】:2015-04-20 00:36:57
【问题描述】:
我有一个架构,我正在尝试将其编译为数据合约。我发现如果一个元素被定义为<xs:element name="DogRequest" type="Dog"></xs:element> 没有为 DogRequest 生成类。我想使用 svcutil 因为我要生成多个命名空间,而 xsd.exe 只允许一个。另外,我有一些使用相同类型的元素,而 xsd.exe 只生成其中一个。有谁知道是否有办法为这个模式生成类?
我正在使用一个接受 XML 有效负载的通用 Web 服务。我希望使用 WCF 来构建消息。
架构
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
targetNamespace="http://tempuri.org/XMLSchema1.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="Dog">
<xs:sequence>
<xs:element name="Name" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="DogRequest" type="Dog"></xs:element>
</xs:schema>
使用svcutil /dconly XMLSchema1.xsd编译
这将为 Dog 生成 1 个类,但不会为 DogRequest 生成一个类。
xsd.exe 将使用 DogRequest 为 Dog 生成 1 个类
svcutil 输出
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="Dog", Namespace="http://tempuri.org/XMLSchema1.xsd")]
public partial class Dog : object, System.Runtime.Serialization.IExtensibleDataObject
{
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private string NameField;
public System.Runtime.Serialization.ExtensionDataObject ExtensionData
{
get
{
return this.extensionDataField;
}
set
{
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, EmitDefaultValue=false)]
public string Name
{
get
{
return this.NameField;
}
set
{
this.NameField = value;
}
}
}
XSD 输出
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/XMLSchema1.xsd")]
[System.Xml.Serialization.XmlRootAttribute("DogRequest", Namespace="http://tempuri.org/XMLSchema1.xsd", IsNullable=false)]
public partial class Dog {
private string nameField;
/// <remarks/>
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
}
【问题讨论】:
标签: c# wcf xsd svcutil.exe