【发布时间】:2025-11-29 19:45:01
【问题描述】:
我正在使用 xsd.exe 从 .xsd 文件生成一些 c# 类。我在此处和其他站点上遇到了相同的问题,其中 xsd.exe 为 .xsd 文件中的类型生成 Type[] 数组而不是通用 List 集合。有人建议,如果将 /dataContractOnly 参数传递给 svcutil.exe,则可以使用 svcutil.exe 作为 xsd.exe 的替代品。但是,这些人似乎误会了,因为 svcutil.exe 实际上生成 System.Xml.XmlNode[] 数组属性,而不是根据 .xsd 文件中的架构创建类型。
例如,给定这个简单的 .xsd 架构:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="Employee">
<xs:all>
<xs:element name="FirstName" type="xs:string"></xs:element>
<xs:element name="LastName" type="xs:string"></xs:element>
</xs:all>
</xs:complexType>
<xs:element name="Employees">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Employee" type="Employee"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
'xsd.exe /classes Example.xsd' 生成:
public partial class Employees {
private Employee[] employeeField;
public Employee[] Employee {
get { return this.employeeField; }
set { this.employeeField = value; }
}
}
public partial class Employee {
private string firstNameField;
private string lastNameField;
public string FirstName {
get { return this.firstNameField; }
set { this.firstNameField = value; }
}
public string LastName {
get { return this.lastNameField; }
set { this.lastNameField = value; }
}
}
'svcutil.exe /target:code /dataContractOnly /serializer:XmlSerializer /importXmlTypes /collectionType:System.Collections.Generic.List`1 Example.xsd' 生成:
public partial class Employee : object, System.Runtime.Serialization.IExtensibleDataObject{
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private string FirstNameField;
private string LastNameField;
public System.Runtime.Serialization.ExtensionDataObject ExtensionData{
get{ return this.extensionDataField; }
set{ this.extensionDataField = value; }
}
public string FirstName{
get{ return this.FirstNameField; }
set{ this.FirstNameField = value; }
}
public string LastName{
get{ return this.LastNameField; }
set{ this.LastNameField = value; }
}
}
public partial class Employees : object, System.Xml.Serialization.IXmlSerializable{
private System.Xml.XmlNode[] nodesField;
private static System.Xml.XmlQualifiedName typeName = new System.Xml.XmlQualifiedName("Employees", "http://tempuri.org/XMLSchema.xsd");
public System.Xml.XmlNode[] Nodes{
get{ return this.nodesField; }
set{ this.nodesField = value; }
}
public void ReadXml(System.Xml.XmlReader reader){
this.nodesField = System.Runtime.Serialization.XmlSerializableServices.ReadNodes(reader);
}
public void WriteXml(System.Xml.XmlWriter writer){
System.Runtime.Serialization.XmlSerializableServices.WriteNodes(writer, this.Nodes);
}
public System.Xml.Schema.XmlSchema GetSchema(){
return null;
}
public static System.Xml.XmlQualifiedName ExportSchema(System.Xml.Schema.XmlSchemaSet schemas){
System.Runtime.Serialization.XmlSerializableServices.AddDefaultSchema(schemas, typeName);
return typeName;
}
}
svcutil.exe 真的应该是 xsd.exe 的替代品吗?生成的输出似乎完全不同。
此时,看来我将不得不使用 xsd.exe 从我的 .xsd 文件创建类,然后手动调整代码以使其成为我想要的形式。我意识到使用纯粹生成的代码是理想的,但我想知道其他人是否使用 xsd.exe 作为起点,然后从那里开始工作,或者我是否需要完全考虑另一种方法?
Visual Studio 2010 中的 xsd.exe 是否有任何更新?
【问题讨论】:
-
svcutil 不是 xsd.exe 的替代品。要尽可能接近,请尝试使用
/dataContractOnly和 '/serializer:XmlSerializer` 开关。 -
ServiceUtil 被
xs:all选项弄糊涂了。如果您的 XSD 使用xs:sequencesvcUtil 将生成正确的输出。来自 datacontract 序列化程序和 svcUtil 的文档:xs:all - Forbidden。 msdn.microsoft.com/en-us/library/ms733112(v=vs.110).aspx -
你可以找到我对同一个问题的回答 [这里][1] [1]:*.com/a/24557248/1125467
标签: c# xml xsd xsd.exe svcutil.exe