Visual Studio comes with a multitude of utilities that can be used to generate code.
As a Middleware I have used XSD.exe a lot to generate classes and Xsd schemas automatically.
How to use
Open a developer Command Prompt
Type xsd, the application should show the list of possibilities proposed by the utility :
D:\Sources\Projects\xsd>xsdMicrosoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.8.3928.0]
Copyright (C) Microsoft Corporation. All rights reserved.
xsd.exe -
Utility to generate schema or class files from given source.
xsd.exe <schema>.xsd /classes|dataset [/e:] [/l:] [/n:] [/o:] [/s] [/uri:]
xsd.exe <assembly>.dll|.exe [/outputdir:] [/type: [...]]
xsd.exe <instance>.xml [/outputdir:]
xsd.exe <schema>.xdr [/outputdir:]
- OPTIONS -
/classes
Generate classes for this schema. Short form is '/c'.
/dataset
Generate sub-classed DataSet for this schema. Short form is '/d'.
/enableLinqDataSet
Generate LINQ-enabled sub-classed Dataset for the schemas provided. Short form is '/eld'.
...
Generate XSD from Xml file
To generate the Xsd schema for a specific xml file, go to the folder containing the sample file.
Sample input sample.xml:
/l is setting the language, there are several languages supported : CS, VB, CPP, JS, … the help of the tool will list all available options.
/n will set the namespace used by within the class.
The tool is generating a class named sample.cs with the following code :
//------------------------------------------------------------------------------// <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>//------------------------------------------------------------------------------// // This source code was auto-generated by xsd, Version=4.8.3928.0.// namespaceappns.xml{usingSystem.Xml.Serialization;/// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://mynamespace.com/sample")] [System.Xml.Serialization.XmlRootAttribute(Namespace="http://mynamespace.com/sample", IsNullable=false)]publicpartialclassroot{privaterootRow[]itemsField;/// <remarks/> [System.Xml.Serialization.XmlElementAttribute("row")]publicrootRow[]Items{get{returnthis.itemsField;}set{this.itemsField=value;}}}/// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://mynamespace.com/sample")]publicpartialclassrootRow{privatestringidField;privatestringparentidField;privatestringnameField;privatestringtypeField;/// <remarks/>publicstringid{get{returnthis.idField;}set{this.idField=value;}}/// <remarks/>publicstringparentid{get{returnthis.parentidField;}set{this.parentidField=value;}}/// <remarks/>publicstringname{get{returnthis.nameField;}set{this.nameField=value;}}/// <remarks/>publicstringtype{get{returnthis.typeField;}set{this.typeField=value;}}}}
The code generated by the tool should never be altered.
The main reason for this being the schema can change, and in that case we have to rerun the automation.
There is a way to add code in a very simple way, to prevent the loss of custom code added in such case.
See Extension Methods
When I am using Visual Studio, I use Pre-build event to generate automatically the code for the schemas I am using. And I dont need to worry about anything changing in my schemas.
For specific code, I am creating new classes with Extension methods and I am never loosing the customization created.
Using multiple XSD files
To generate code from multiple xsd files, it is exactly the same command with the list of all schemas, first the referenced schemas, and last the main schema.
This xsd tool is really fast and easy to generate code from XML file or XSD schemas.
It can also be used to automate the generation at compilation time with the pre-build events.