jam*_*lle 22 c# xml xsd xsd.exe svcutil.exe
我正在使用xsd.exe从.xsd文件生成一些c#类.我遇到了此处和其他网站上涉及的相同问题,其中xsd.exe生成Type []数组,而不是.xsd文件中类型的通用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>
Run Code Online (Sandbox Code Playgroud)
'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; }
}
}
Run Code Online (Sandbox Code Playgroud)
'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;
}
}
Run Code Online (Sandbox Code Playgroud)
svcutil.exe真的应该是xsd.exe的替代品吗?产生的输出似乎完全不同.
此时,看起来我将不得不使用xsd.exe从我的.xsd文件创建类,然后手动调整代码以获得我想要的形式.我意识到使用纯粹生成的代码是理想的,但我想知道是否有其他人使用xsd.exe作为起点然后在那里工作或者我是否需要考虑另一种方法?
Visual Studio 2010中是否有对xsd.exe的更新?
是的,svcutil.exe 可以用作替代品,xsd.exe但听起来您无法生成通用集合. svcutil.exe有一个collectionType开关,允许您指定要用于集合的类型:
svcutil /o:Svc.cs /ct:System.Collections.Generic.List`1 http://example.com
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19023 次 |
| 最近记录: |