C# 如何从类创建 XSD 架构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10017139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:08:45  来源:igfitidea点击:

How to create a XSD schema from a class?

c#xmlxsdxsd.exelinq-to-xsd

提问by Darf Zon

I'm having a hard time with the XSD files.

我很难处理 XSD 文件。

I'm trying to create an XSD file from a class:

我正在尝试从一个类创建一个 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
{
    // ...
}

Please note that I have an abstract class inside of Configuration. With that feature, is it possible to create the XSD and if it's possible how?

请注意,我在 Configuration 中有一个抽象类。使用该功能,是否可以创建 XSD,如果可能,如何创建?

The idea is to pass the class Configuration to the XSD.

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

采纳答案by Alex

You can use XSD.exe(Available from your Visual Studio Installation.)

您可以使用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 {   }

results in

结果是

<?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>

All you have to do is compiling your assembly and run XSD.exewith the path to your assembly as argument. XSD.exe /?has a list of all arguments as well.

您所要做的就是编译您的程序集并XSD.exe使用程序集的路径作为参数运行。XSD.exe /?也有所有参数的列表。

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

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

回答by toddmo

You can successfully integrate xsd.exeinto the Visual Studio IDE like this:

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

Go into Tools, External Toolsand click the Addbutton:

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

2010

2010年

enter image description here

在此处输入图片说明

2015 / 2017

2015 / 2017

enter image description here

在此处输入图片说明

Title:

标题:

Create Schema From Class

从类创建架构

Command (per framework):

命令(每个框架):

4.0

4.0

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

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

4.5.1

4.5.1

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

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

4.6.*

4.6.*

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

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

Arguments:

参数:

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

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

Use Output window:

使用输出窗口:

Prevents an extra command window from popping up and keeps a record of the output until you clear it. Probably a good idea.

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

Prompt For Arguments:

提示参数:

Check if you want to test the output or troubleshoot; otherwise, leave unchecked.

检查是否要测试输出或故障排除;否则,不要选中。

Click OK

点击 OK

How to use:

如何使用:

  1. Compile your project!XSD.exeonly looks at compiled code.
  2. Click on the class in Solution Explorer.
  3. Click Tools, Create Schema From Class
  4. Click on the Show All Filesbutton in the Solution Explorer.
  5. Look in the same folder as your class and you will see Schema0.xsd.
  6. Right-click on Schema0.xsdand choose Include In Project
  7. Rename Schema0.xsdto <the name of the class>.xsd
  8. (optional) You may have to edit this new xsdby hand if you want to edit xml files in the xml editor using this schema and you are not using all attributes. You can replace use="required"with use="optional"to get rid of the blue squiggly lines in the xml editor (which create warnings), if indeed these attributes are not required.
  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编辑器的蓝色波浪线(其创建的警告),如果不是确实需要这些属性。