我在使用XSD文件时遇到了麻烦。
我试图从一个类创建一个XSD文件:
public enum Levels { Easy, Medium, Hard } public sealed class Configuration { public string Name { get;set; } public Levels Level { get; set; } public ConfigurationSpec { get;set;} } public abstract class ConfigurationSpec { } public class ConfigurationSpec1 { // ... } public class ConfigurationSpec2 { // ... }
请注意,我在Configuration中有一个抽象类。有了该功能,是否可以创建XSD?如果可以,如何创建?
想法是将类Configuration传递给XSD。
您可以使用XSD.exe(可从Visual Studio安装中获得。)
XSD.exe
public sealed class Configuration { public string Name { get; set; } public Levels Level { get; set; } public ConfigurationSpec Spec { get; set; } } public abstract class ConfigurationSpec { } public class ConfigurationSpec1 { } public class ConfigurationSpec2 { }
结果是
<?xml version="1.0" encoding="utf-8"?> <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Levels" type="Levels" /> <xs:simpleType name="Levels"> <xs:restriction base="xs:string"> <xs:enumeration value="Easy" /> <xs:enumeration value="Medium" /> <xs:enumeration value="Hard" /> </xs:restriction> </xs:simpleType> <xs:element name="Configuration" nillable="true" type="Configuration" /> <xs:complexType name="Configuration"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" /> <xs:element minOccurs="1" maxOccurs="1" name="Level" type="Levels" /> <xs:element minOccurs="0" maxOccurs="1" name="Spec" type="ConfigurationSpec" /> </xs:sequence> </xs:complexType> <xs:complexType name="ConfigurationSpec" abstract="true" /> <xs:element name="ConfigurationSpec" nillable="true" type="ConfigurationSpec" /> <xs:element name="ConfigurationSpec1" nillable="true" type="ConfigurationSpec1" /> <xs:complexType name="ConfigurationSpec1" /> <xs:element name="ConfigurationSpec2" nillable="true" type="ConfigurationSpec2" /> <xs:complexType name="ConfigurationSpec2" /> </xs:schema>
您要做的就是编译程序集,并XSD.exe以程序集的路径作为参数运行。XSD.exe /?也具有所有参数的列表。
XSD.exe /?
例: XSD.exe C:\Dev\Project1\Bin\Debug\library.dll
XSD.exe C:\Dev\Project1\Bin\Debug\library.dll