In this article, I have explained how you can generate XSD to C# class using a command prompt (using xsd.exe), using Visual Studio 2017 or later, and I have also explained a step-by-step procedure to get C# class from XSD, both the ways, with example.
Table of Contents
C# XSD to Class using Command Prompt
Let's take a Command prompt solution first, in which we will use xsd.exe Here are the steps to follow:
- First of all, we would need to have (XML Schema Definition) XSD.exe in order to covert xsd to class, so if you have already installed Visual Studio it will be automatically installed and comes as part of the Windows Software Development Kit.
- You can find you xsd.exe in the following location (depending upon .net version installed on your pc):
So, it depends on your .NET version installed, but basically naviagte to "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin" check you .NET version and you find xsd.exe there.C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\xsd.exe C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7 Tools\xsd.exe C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.1 Tools\xsd.exe
- I have found my local xsd.exe "
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools
" - Now, search "CMD" in your windows and right-click, select "Run as administrator"
- Navigate to above location using cmd
C:\Windows\system32>cd C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools
- Now, we need to run command as below to convert XSD into Class
xsd E:\XSDtoClass\sample.xsd /classes /outputdir:E:\XSDToClass
Where
xsd
is command,E:\XSDtoClass\sample.xsd
is location of .xsd file which we need to convert/classes
= we need to convert to class/outputdir:E:\XSDToClass
= location of sample.cs file, after conversion
Sample.XSD File<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/PurchaseOrderSchema.xsd" targetNamespace="http://tempuri.org/PurchaseOrderSchema.xsd" elementFormDefault="qualified"> <xsd:element name="PurchaseOrder" type="tns:PurchaseOrderType"/> <xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="ShipTo" type="tns:USAddress" maxOccurs="2"/> <xsd:element name="BillTo" type="tns:USAddress"/> </xsd:sequence> <xsd:attribute name="OrderDate" type="xsd:date"/> </xsd:complexType> <xsd:complexType name="USAddress"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:integer"/> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/> </xsd:complexType> </xsd:schema>
So complete comamnd looks like this
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools>xsd E:\XSDtoClass\sample.xsd /classes /outputdir:E:\XSDToClass?
- Output of sample C# class
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System.Xml.Serialization; // // This source code was auto-generated by xsd, Version=4.6.1055.0. // /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/PurchaseOrderSchema.xsd")] [System.Xml.Serialization.XmlRootAttribute("PurchaseOrder", Namespace="http://tempuri.org/PurchaseOrderSchema.xsd", IsNullable=false)] public partial class PurchaseOrderType { private USAddress[] shipToField; private USAddress billToField; private System.DateTime orderDateField; private bool orderDateFieldSpecified; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("ShipTo")] public USAddress[] ShipTo { get { return this.shipToField; } set { this.shipToField = value; } } /// <remarks/> public USAddress BillTo { get { return this.billToField; } set { this.billToField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute(DataType="date")] public System.DateTime OrderDate { get { return this.orderDateField; } set { this.orderDateField = value; } } /// <remarks/> [System.Xml.Serialization.XmlIgnoreAttribute()] public bool OrderDateSpecified { get { return this.orderDateFieldSpecified; } set { this.orderDateFieldSpecified = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/PurchaseOrderSchema.xsd")] public partial class USAddress { private string nameField; private string streetField; private string cityField; private string stateField; private string zipField; private string countryField; public USAddress() { this.countryField = "US"; } /// <remarks/> public string name { get { return this.nameField; } set { this.nameField = value; } } /// <remarks/> public string street { get { return this.streetField; } set { this.streetField = value; } } /// <remarks/> public string city { get { return this.cityField; } set { this.cityField = value; } } /// <remarks/> public string state { get { return this.stateField; } set { this.stateField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(DataType="integer")] public string zip { get { return this.zipField; } set { this.zipField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute(DataType="NMTOKEN")] public string country { get { return this.countryField; } set { this.countryField = value; } } }
If you have multiple connected XSD files to convert you can execute command like below
xsd schema1.xsd schema2.xsd schema3.xsd /classes
For more useful command related to XSD check this link: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/x6c1kb0s(v=vs.100)?redirectedfrom=MSDN
XSD to Class using Visual Studio
You can also convert XSD to class using Visual Studio, I am using Visual Studio 2017, but you can use VS 2017 or above for it, steps will remain same, here are the steps to do it
- Open your XSD file in your Visual Studio
- Now, in your Visual Studio, once you have your XSD file, click on "XML schema explorer", as shown in the below image
- Now, In "XML Schema Explorer" 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.
- XML file will be generated, select all XML and copy the XML code, but we need Class file.
- So we will create a Sample.Cs ( C# class ) inside the Visual Studio solution
- Now, we will navigate to "Edit"-> "Paste Special" -> Select "Paste XML as Classes" as shown in the below image
- You will see class file with all properties, here is the sample output
That's it, hope it helps.
You might also like to read:
Quick Tip to convert JSON into class object
Best Java IDE for making easy to code in Java. (Eclipse, Netbeans or Any other?)