How to generate .NET 4.0 classes from xsd?

C#.NetXsd

C# Problem Overview


What are the options to generate .NET 4.0 c# classes (entities) from an xsd file, using Visual Studio 2010?

C# Solutions


Solution 1 - C#

simple enough; just run (at the vs command prompt)

xsd your.xsd /classes

(which will create your.cs). Note, however, that most of the intrinsic options here haven't changed much since 2.0

For the options, use xsd /? or see MSDN; for example /enableDataBinding can be useful.

Solution 2 - C#

xsd.exe as mentioned by Marc Gravell. The fastest way to get up and running IMO.

Or if you need more flexibility/options :

xsd2code VS add-in (Codeplex)

Solution 3 - C#

I show you here the easiest way using Vs2017 and Vs2019 Open your xsd with Visual Studio and generate a sample xml file as in the url suggested.

Once you opened your xsd in design view as below, click on xml schema explorer enter image description here

Within “XML Schema Explorer” scroll all the way down to find the root/data node. Right click on root/data node and it will show “Generate Sample XML”. If it does not show, it means you are not on the data element node but you are on any of the data definition node.

enter image description here

  1. Copy your generated Xml into the clipboard
  2. Create a new empty class in your solution and delete the class definition. Only Namespace should remain
  3. While your mouse pointer focused inside your class, choose EDIT-> Paste Special-> Paste Xml as Classes

Solution 4 - C#

xsd.exe does not work well when you have circular references (ie a type can own an element of its own type directly or indirectly).

When circular references exist, I use Xsd2Code. Xsd2Code handles circular references well and works within the VS IDE, which is a big plus. It also has a lot of features you can use like generating the serialization/deserialization code. Make sure you turn on the GenerateXMLAttributes if you are generating serialization though (otherwise you'll get exceptions for ordering if not defined on all elements).

Neither works well with the choice feature. you'll end up with lists/collections of object instead of the type you want. I'd recommend avoiding choice in your xsd if possible as this does not serialize/deserialize well into a strongly typed class. If you don't care about this, though, then it's not a problem.

The any feature in xsd2code deserializes as System.Xml.XmlElement which I find really convenient but may be an issue if you want strong typed objects. I often use any when allowing custom config data, so an XmlElement is convenient to pass to another XML deserializer that is custom defined elsewhere.

Solution 5 - C#

For a quick and lazy solution, (and not using VS at all) try these online converters:

  • xsd-to-xml-converter here
  • xmltocsharp converter here

> XSD => XML => C# classes

Example XSD:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder">
  <xs:complexType>
	<xs:sequence>
	  <xs:element name="orderperson" type="xs:string"/>
	  <xs:element name="shipto">
		<xs:complexType>
		  <xs:sequence>
			<xs:element name="name" type="xs:string"/>
			<xs:element name="address" type="xs:string"/>
			<xs:element name="city" type="xs:string"/>
			<xs:element name="country" type="xs:string"/>
		  </xs:sequence>
		</xs:complexType>
	  </xs:element>
	  <xs:element name="item" maxOccurs="unbounded">
		<xs:complexType>
		  <xs:sequence>
			<xs:element name="title" type="xs:string"/>
			<xs:element name="note" type="xs:string" minOccurs="0"/>
			<xs:element name="quantity" type="xs:positiveInteger"/>
			<xs:element name="price" type="xs:decimal"/>
		  </xs:sequence>
		</xs:complexType>
	  </xs:element>
	</xs:sequence>
	<xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>

</xs:schema>

Converts to XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<shiporder xsi:noNamespaceSchemaLocation="schema.xsd" orderid="string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <orderperson>string</orderperson>
  <shipto>
	<name>string</name>
	<address>string</address>
	<city>string</city>
	<country>string</country>
  </shipto>
  <item>
	<title>string</title>
	<note>string</note>
	<quantity>3229484693</quantity>
	<price>-6894.465094196054907</price>
  </item>
  <item>
	<title>string</title>
	<note>string</note>
	<quantity>2181272155</quantity>
	<price>-2645.585094196054907</price>
  </item>
  <item>
	<title>string</title>
	<note>string</note>
	<quantity>2485046602</quantity>
	<price>4023.034905803945093</price>
  </item>
  <item>
	<title>string</title>
	<note>string</note>
	<quantity>1342091380</quantity>
	<price>-810.825094196054907</price>
  </item>
</shiporder>

Which converts to this class structure:

   /* 
	Licensed under the Apache License, Version 2.0
	
	http://www.apache.org/licenses/LICENSE-2.0
	*/
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
	[XmlRoot(ElementName="shipto")]
	public class Shipto {
		[XmlElement(ElementName="name")]
		public string Name { get; set; }
		[XmlElement(ElementName="address")]
		public string Address { get; set; }
		[XmlElement(ElementName="city")]
		public string City { get; set; }
		[XmlElement(ElementName="country")]
		public string Country { get; set; }
	}

	[XmlRoot(ElementName="item")]
	public class Item {
		[XmlElement(ElementName="title")]
		public string Title { get; set; }
		[XmlElement(ElementName="note")]
		public string Note { get; set; }
		[XmlElement(ElementName="quantity")]
		public string Quantity { get; set; }
		[XmlElement(ElementName="price")]
		public string Price { get; set; }
	}

	[XmlRoot(ElementName="shiporder")]
	public class Shiporder {
		[XmlElement(ElementName="orderperson")]
		public string Orderperson { get; set; }
		[XmlElement(ElementName="shipto")]
		public Shipto Shipto { get; set; }
		[XmlElement(ElementName="item")]
		public List<Item> Item { get; set; }
		[XmlAttribute(AttributeName="noNamespaceSchemaLocation", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
		public string NoNamespaceSchemaLocation { get; set; }
		[XmlAttribute(AttributeName="orderid")]
		public string Orderid { get; set; }
		[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
		public string Xsi { get; set; }
	}

}

Attention! Take in account that this is just to Get-You-Started, the results obviously need refinements!

Solution 6 - C#

I use XSD in a batch script to generate .xsd file and classes from XML directly :

set XmlFilename=Your__Xml__Here
set WorkingFolder=Your__Xml__Path_Here

set XmlExtension=.xml
set XsdExtension=.xsd

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

set XmlFilePath=%WorkingFolder%%XmlFilename%%XmlExtension%
set XsdFilePath=%WorkingFolder%%XmlFilename%%XsdExtension%

%XSD% %XmlFilePath% /out:%WorkingFolder%
%XSD% %XsdFilePath% /c /out:%WorkingFolder%

Solution 7 - C#

The command that worked in my case was:

xsd /c your.xsd 

Solution 8 - C#

I used xsd.exe in the Windows command prompt.

However, since my xml referenced several online xml's (in my case http://www.w3.org/1999/xlink.xsd which references http://www.w3.org/2001/xml.xsd) I had to also download those schematics, put them in the same directory as my xsd, and then list those files in the command:

> "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\xsd.exe" /classes /language:CS your.xsd xlink.xsd xml.xsd

Solution 9 - C#

Marc Gravells answer was right for me but my xsd was with extension of .xml. When I used xsd program it gave :
- The table (Amt) cannot be the child table to itself in nested relations.

As per this KB325695 I renamed extension from .xml to .xsd and it worked fine.

Solution 10 - C#

Along with WSDL, I had xsd files. The above did not work in my case gave error. It worked as follows

wsdl /l:C# /out:D:\FileName.cs D:\NameApi\wsdl_1_1\RESAdapterService.wsdl 
D:\CXTypes.xsd D:\CTypes.xsd 
D:\Preferences.xsd 

Solution 11 - C#

If you want to generate the class with auto properties, convert the XSD to XML using this then convert the XML to JSON using this and copy to clipboard the result. Then in VS, inside the file where your class will be created, go to Edit>Paste Special>Paste JSON as classes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBastien VandammeView Question on Stackoverflow
Solution 1 - C#Marc GravellView Answer on Stackoverflow
Solution 2 - C#Serge WautierView Answer on Stackoverflow
Solution 3 - C#EmilView Answer on Stackoverflow
Solution 4 - C#VoteCoffeeView Answer on Stackoverflow
Solution 5 - C#cnomView Answer on Stackoverflow
Solution 6 - C#hdoghmenView Answer on Stackoverflow
Solution 7 - C#MelchiaView Answer on Stackoverflow
Solution 8 - C#Aske B.View Answer on Stackoverflow
Solution 9 - C#Igoris AzanovasView Answer on Stackoverflow
Solution 10 - C#Hamit YILDIRIMView Answer on Stackoverflow
Solution 11 - C#jcsilva87View Answer on Stackoverflow