How to create a XSD schema from a class?
Asked Answered
P

2

62

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.

Plus answered 4/4, 2012 at 18:27 Comment(1)
You can use the free XML Schema Definition Tool (Xsd.exe).Kidding
C
36

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

Coriecorilla answered 4/4, 2012 at 18:49 Comment(7)
Can you tell me which steps did you follow to generated it?Plus
It throws me: Error - Could not load file or assembly 'file:///C:/../test.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format.Plus
@DarfZon try to change it to the same architecture as your OS (x64, x86).Corregidor
If anyone is still wondering, to choose a specific class to generate an xsd, type: xsd.exe C:\Dev\Project1\Bin\Debug\library.dll /t:<projectName>.<classNameWithout .cs> This should produce a schema from the code. You can specify the location of the output using the /out:<location> command.Arose
@BradGermain, it should be /t:<fully qualified namespace>.<className without .cs>, but thanks for the tipKreager
Is their anyway of doing this in code as I have a project where i need run in browser but gen xsd'sSyllabize
Is there any way to add also <summary> of properties from class to xsd? maybe in xsd it will <documentation>Imaginary
S
103

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

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

2010

enter image description here

2015 / 2017

enter image description here

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.
Swen answered 26/10, 2014 at 17:24 Comment(13)
Don't forget step 1 Compile, during usage, as I did. Took me some time to realize :$Maleate
@R.Schreurs, emphasis added. Thanks!Swen
This answer is very nice. Thanks. In your opinion is possible to add the comments wrote in the class also in the XSD output file?Margertmargery
@theLaw, unfortunately, xsd.exe does not offer that as a command line argument. msdn.microsoft.com/en-us/library/…Swen
Error: Error while processing 'D:\Lavori\Amplifon\CRM Wrapper ES\Source\EAI.CRM.Xrm\bin\Test\EAI.CRM.Xrm.dll'. - There was an error reflecting type 'EAI.CRM.Xrm.Configuration.SoapLoggerConfiguration'. - You must implement a default accessor on System.Configuration.ConfigurationLockCollection because it inherits from ICollectionCashandcarry
@GlaucoCucchiar, this error is a serialization error, not really related to xsd.exe. See https://mcmap.net/q/323493/-xmlserialize-exception or search for "You must implement a default accessor on System.Configuration.ConfigurationLockCollection because it inherits from ICollection" on StackOverflow.Swen
Warning: cannot generate schemas because no suitable types were found.Alleged
@PaulMcCarthy, see #3055187 and / or search S/O on that error. It's fixable.Swen
how to generate for separate files for more than one classConfabulate
@ragavan, just follow these steps, for one class at a time, and you will have one xsd file per class.Swen
Try TargetPath instead of BinDir and TargetName. Also some extensions are things like EXE and not DLL, so it's best to use TargetExt instead of a hardcoded .dllPropitiatory
Step 3 is different from when you added the tool. Once you add, your new tool title shows up in the Tools menu. Don't go to External Tools. That one got me for a minute or two.Motoring
This is awesome. So much easier than using a Command Prompt outside of VS. Thanks.Motoring
C
36

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

Coriecorilla answered 4/4, 2012 at 18:49 Comment(7)
Can you tell me which steps did you follow to generated it?Plus
It throws me: Error - Could not load file or assembly 'file:///C:/../test.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format.Plus
@DarfZon try to change it to the same architecture as your OS (x64, x86).Corregidor
If anyone is still wondering, to choose a specific class to generate an xsd, type: xsd.exe C:\Dev\Project1\Bin\Debug\library.dll /t:<projectName>.<classNameWithout .cs> This should produce a schema from the code. You can specify the location of the output using the /out:<location> command.Arose
@BradGermain, it should be /t:<fully qualified namespace>.<className without .cs>, but thanks for the tipKreager
Is their anyway of doing this in code as I have a project where i need run in browser but gen xsd'sSyllabize
Is there any way to add also <summary> of properties from class to xsd? maybe in xsd it will <documentation>Imaginary

© 2022 - 2024 — McMap. All rights reserved.