How to create a XSD schema from a class?

巧了我就是萌 提交于 2019-12-17 03:54:18

问题


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

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

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?

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


回答1:


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

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.exe with the path to your assembly as argument. XSD.exe /? has a list of all arguments as well.

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




回答2:


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

Go into Tools, External Tools and click the Add button:

2010

2015 / 2017

Title:

Create Schema From Class

Command (per framework):

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

Arguments:

$(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

How to use:

  1. Compile your project! XSD.exe only 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 Files button in the Solution Explorer.
  5. Look in the same folder as your class and you will see Schema0.xsd.
  6. Right-click on Schema0.xsd and choose Include In Project
  7. Rename Schema0.xsd to <the name of the class>.xsd
  8. (optional) You may have to edit this new xsd by 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.


来源:https://stackoverflow.com/questions/10017139/how-to-create-a-xsd-schema-from-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!