2012-04-04 115 views
38

我很难与XSD文件。如何从类创建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 
{ 
    // ... 
} 

请注意,我所拥有的配置里面的抽象类。有了这个功能,是否有可能创建XSD,如果可能的话,该怎么做?

这个想法是将类Configuration传递给XSD。

+0

您可以使用免费的[XML架构定义工具(Xsd.exe)](http://msdn.microsoft.com/zh-cn/library/x6c1kb0s.aspx)。 – 2012-04-04 18:45:12

回答

26

您可以使用XSD.exe(从您的Visual Studio安装可用。)

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 C:\Dev\Project1\Bin\Debug\library.dll

+1

你能告诉我你遵循了哪些步骤来生成它? – 2012-04-04 18:57:15

+0

它引发我:错误 - 无法加载文件或程序集'file:/// C:/../test.exe'或它的一个依赖项。试图加载格式不正确的程序。 – 2012-04-04 19:52:50

+1

@DarfZon尝试将其更改为与您的操作系统(x64,x86)相同的体系结构。 – Peter 2013-08-02 09:08:09

57

可以成功整合xsd.exe到Visual Studio IDE这样的:

进入Tools, External Tools并单击添加按钮:

enter image description here

二千零十七分之二千零一十五

enter image description here

标题:

创建模式从阶级

命令(每框架):

4.0

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\xsd.exe

4.5.1

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\xsd.exe

4.6。*

C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.* Tools\x64\xsd.exe

参数:

$(BinDir)$(TargetName).dll /outputdir:$(ItemDir) /type:$(ItemFileName)

使用输出窗口:

从弹出防止额外的命令窗口,并保持输出的记录,直到你清除它。可能是一个好主意。

提示输入参数:

检查,如果你要测试的输出或解决;否则,请不要选中。

点击OK

使用方法:

  1. 编译您的项目!XSD.exe只看编译的代码。
  2. 点击在Solution Explorer中类。
  3. 点击Tools, Create Schema From Class
  4. 点击显示在Solution Explorer中所有文件按钮。
  5. 查看与你班级相同的文件夹,你会看到Schema0.xsd
  6. Schema0.xsd右键单击并选择Include In Project
  7. 重命名Schema0.xsd<the name of the class>.xsd
  8. (可选)你也可以通过手工编辑这个新xsd,如果你想用这个模式在XML编辑器编辑XML文件和你没有使用所有属性。如果确实不需要这些属性,您可以使用use="optional"替换use="required"以消除xml编辑器中的蓝色波浪线(创建警告)。
+2

不要忘记步骤** 1在使用过程中编译**,就像我一样。花了我一些时间才意识到:$ – 2016-02-25 12:55:12

+1

@ R.Schreurs,强调补充。谢谢! – toddmo 2016-02-25 18:44:38

+0

这个回答很好。谢谢。你认为可以在XSD输出文件中添加写在类中的注释吗? – theLaw 2016-11-03 09:18:56