如何从类创建XSD架构?

Dar*_*Zon 49 c# xml xsd xsd.exe linq-to-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
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

请注意,我在Configuration中有一个抽象类.有了这个功能,是否有可能创建XSD,如果可能的话?

我们的想法是将类配置传递给XSD.

tod*_*dmo 80

您可以xsd.exe像这样成功地集成到Visual Studio IDE中:

进入Tools, External Tools并单击Add按钮:

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)

使用输出窗口:

防止弹出额外的命令窗口并保留输出记录,直到您清除它为止.可能是个好主意.

提示参数:

检查您是否要测试输出或进行故障排除; 否则,不加以控制.

点击 OK

如何使用:

  1. 编译你的项目! XSD.exe只查看已编译的代码.
  2. 解决方案资源管理器中单击该类.
  3. 点击 Tools, Create Schema From Class
  4. 单击解决方案资源管理器中Show All Files按钮.
  5. 查看与您的班级相同的文件夹,您将看到Schema0.xsd.
  6. 右键单击Schema0.xsd并选择Include In Project
  7. 重命名Schema0.xsd<the name of the class>.xsd
  8. (可选)xsd如果要使用此架构在xml编辑器中编辑xml文件,并且未使用所有属性,则可能需要手动编辑此新文件.您可以替换use="required"use="optional"摆脱在XML编辑器的蓝色波浪线(其创建的警告),如果不是确实需要这些属性.

  • 不要忘记步骤**1编译**,在使用过程中,就像我一样.花了我一些时间才意识到:$ (4认同)
  • 警告:无法生成模式,因为找不到合适的类型。 (2认同)

Ale*_*lex 34

您可以使用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 {   }
Run Code Online (Sandbox Code Playgroud)

结果是

<?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>
Run Code Online (Sandbox Code Playgroud)

您所要做的就是编译程序集并XSD.exe以程序集的路径作为参数运行.XSD.exe /?还有一个所有参数的列表.

例: XSD.exe C:\Dev\Project1\Bin\Debug\library.dll

  • 如果还有人想知道,要选择一个特定的类来生成xsd,请键入:**xsd.exe C:\ Dev\Project1\Bin\Debug\library.dll /t:<projectName>.<classNameWithout .cs>**这应该从代码中生成一个模式.您可以使用/ out:<location>命令指定输出的位置. (12认同)
  • @BradGermain,它应该是**/t:<完全限定的命名空间>.<className without .cs>**,但感谢提示 (5认同)
  • @DarfZon 尝试将其更改为与您的操作系统(x64、x86)相同的架构。 (2认同)