How to Create a Xsd Schema from a Class

How to create a XSD schema from a class?

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

XSD generation from class, from code

how to generate xsd from class in code

Using XSD.exe.

without XSD.exe?

Why? Then you'll have to reinvent the wheel, or use a library.

utility to generate xsd from java class

If you're already using JAXB, you can use the schemagen tool for creating an XSD:

  • http://docs.oracle.com/javase/6/docs/technotes/tools/share/schemagen.html
  • http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.express.doc%2Finfo%2Fexp%2Fae%2Ftwbs_jaxbjava2schema.html

There are also Ant tasks and Maven plugins for doing the same in an automated fashion.

vb.net - Create Class from XSD and generate xml

If you want to create a XML document that is based on a XSD there are a few steps you need to walk through.

1) You will need to create .NET classes based on your XSD.

2) You will need to create a new instance of that class and serialize the output.

Step 1 - Create a .NET Class from a XSD Document

A XSD file provides the blue print for a class. Here is a example of a XSD file:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Person" nillable="true" type="Person" />
<xs:complexType name="Person">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="firstName" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="lastName" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="dateOfBirth" type="xs:dateTime" />
<xs:element minOccurs="1" maxOccurs="1" name="gender" type="Gender" />
<xs:element minOccurs="1" maxOccurs="1" name="height" type="xs:int" />
<xs:element minOccurs="1" maxOccurs="1" name="weight" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="Gender">
<xs:restriction base="xs:string">
<xs:enumeration value="Male" />
<xs:enumeration value="Female" />
</xs:restriction>
</xs:simpleType>
</xs:schema>

Create a new folder to work in. I'm using 'C:\STACK'.

Create new text file, copy and paste the XSD into it and save it as 'person.xsd'.

Now we need to use the XSD.exe to convert this file into a class.

You will need to find the XSD exe on your machine, for me it was in:

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\xsd.exe

Now open command prompt and enter this

cd "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools"

Now we will create the .NET classes (here is is command broken down)

xsd.exe             -Execute xsd   
/classes -Create Clasess
/language:vb -Language to use (VB, CS, JS)
/out:"c:\stack\" -Output folder
c:\stack\person.xsd -The XSD File to use

Here is the command in one line

xsd.exe c:\stack\person.xsd /classes /language:vb /out:c:\stack\  

After you have run this command a new file will be created 'c:\stack\person.vb'
You can then add this class into you project.

Step 2 - Create a new instance of that class and serialize the output

Now that you have added the new class, you can create a instance of it:

    Dim person As New  Person
person.firstName = "Mike"
person.lastName = "Bateman"
person.gender = Gender.Male
person.height = 160
person.weight = 80.3

Now we can serialize the class to a XML file:

    Dim serializer As New XmlSerializer(GetType(Person))
Dim writer As New StreamWriter("c:\stack\person.xml")
serializer.Serialize(writer, person)
writer.Close()

And we can read the XML back to a .NET class like this:

    Dim serializer As New XmlSerializer(GetType(Person))
Dim reader As New IO.StreamReader("c:\stack\person.xml")
Dim personRes As Person = serializer.Deserialize(reader)
reader.Close()
reader.Dispose()

Hope that helps!

Generate Java classes from .XSD files...?

JAXB does EXACTLY what you want. It's built into the JRE/JDK starting at 1.6



Related Topics



Leave a reply



Submit