我在使用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,如果可能的话?
我们的想法是将类配置传递给XSD。
答案 0 :(得分:76)
您可以成功将xsd.exe
集成到Visual Studio IDE中,如下所示:
进入Tools, External Tools
并点击添加按钮:
<强> 2010 强>
2015/2017
<强>标题强>
从类创建架构
命令(每个框架):
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)
使用输出窗口:
防止弹出额外的命令窗口并保留输出记录,直到您清除它为止。可能是一个好主意。
提示参数:
检查您是否要测试输出或进行故障排除;否则,请不要选中。
单击确定
使用方法:
XSD.exe
只查看已编译的代码。Tools, Create Schema From Class
Schema0.xsd
。 Schema0.xsd
,然后选择Include In Project
Schema0.xsd
重命名为<the name of the class>.xsd
xsd
。如果确实不需要这些属性,您可以将use="required"
替换为use="optional"
以消除xml编辑器中的蓝色波浪线(这会产生警告)。答案 1 :(得分:32)
您可以使用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