A native, cross-platform tool for generating C# classes from XSD schema files, producing output functionally identical to Microsoft's legacy xsd.exe tool.
This tool provides a command-line interface for converting XSD (XML Schema Definition) files to C# classes. It uses System.Xml.Schema for parsing and System.CodeDom for code generation, ensuring cross-platform compatibility while maintaining backward compatibility with xsd.exe-generated code.
This tool was created to replace the legacy xsd.exe tool used in the Firely CQL SDK. The previous approach relied on the Windows-only .NET Framework tool, which:
- Only runs on Windows
- Cannot be customized or extended
- Is no longer actively developed
- Uses .NET Framework-only APIs (
XmlSchemaImporter/XmlCodeExporter)
This new tool provides:
- ✅ Cross-platform support (Windows, Linux, macOS)
- ✅ Backward compatibility - generates functionally identical code to xsd.exe for ELM schemas
- ✅ Feature parity for ELM needs - arrays, inheritance, enums, attributes, abstract types
- ✅ Modern .NET APIs - uses System.CodeDom available on all platforms
- ✅ Future extensibility for nullable annotations and modern C# features
- ✅ Open source and actively maintained
- .NET 8.0 or later (to build and run the tool)
- No additional dependencies
To build the tool:
cd tools/XsdToCSharpConverter
dotnet buildFor a release build:
dotnet build -c ReleaseOr publish as a self-contained executable:
dotnet publish -c Release -r win-x64 --self-contained
dotnet publish -c Release -r linux-x64 --self-contained
dotnet publish -c Release -r osx-x64 --self-containeddotnet xsd2cs.dll [options] schema1.xsd [schema2.xsd ...]Or if published as self-contained:
xsd2cs [options] schema1.xsd [schema2.xsd ...]/c- Generate classes (default mode)/o:<path>- Output directory for generated files/n:<namespace>- Namespace for generated classes/out:<filename>- Output filename (default: derived from first schema file)
Generate C# classes (compatible mode, matches xsd.exe output):
dotnet xsd2cs.dll /c /o:.. /n:Hl7.Cql.Elm library.xsd expression.xsdGenerate with custom output filename:
dotnet xsd2cs.dll /c /o:.. /n:Hl7.Cql.Elm /out:elmv2.g.cs library.xsd expression.xsd- Parse XSD Files: Loads and compiles one or more XSD schema files using
System.Xml.Schema - Build Type Model: Extracts complex types, simple types, elements, and attributes from the schemas
- Generate C# Code: Creates C# classes, properties, enums, and attributes using
System.CodeDom - Apply Customizations: Adds XML serialization attributes and applies generation mode (compatible or modern)
- Write Output: Writes formatted C# code to the specified output file
Generates C# code that matches the output of the legacy xsd.exe tool:
- ✅ Full type support: Complex types, simple types, enums, inheritance
- ✅ Complete attribute generation: XmlElement, XmlArray, XmlArrayItem, XmlAttribute, XmlRoot, XmlInclude, DefaultValue
- ✅ Abstract types: Properly marks abstract XSD types as abstract C# classes
- ✅ Array handling: Both array wrapper patterns and direct array elements
- ✅ Default values: Constructor initialization for fields with defaults
- ✅
*Specifiedpattern: Boolean flags for optional value type attributes - ✅ Mixed content: Support for IsMixed complex types with Text property
- ✅ Type ordering: Root element types first, then document order
- ✅ Exact formatting: Block bracing style matching xsd.exe output
- ✅ JSON discriminator conflict handling: Automatically adds
[JsonIgnore]to properties named"type"with complex types to avoid conflicts with JSON polymorphic type discriminators
Potential features for future versions:
- .NET 10 serialization support - Update generated code for .NET 10's enhanced serialization features
- Nullable reference type annotations (Phase 2)
- Modern C# features: record types, init properties, collection expressions (Phase 3)
- Custom code generation templates (Phase 4)
The Cql/Elm directory contains a script that uses this tool:
Windows:
cd Cql\Elm
Elm.g.cs-Generate-xsd2cs.cmdThis script automatically builds the tool if needed, runs the generation, and adds the appropriate file header.
This tool is designed to work on all platforms supported by .NET 8.0:
- ✅ Windows (x64, x86, ARM64)
- ✅ Linux (x64, ARM64)
- ✅ macOS (x64, ARM64)
No platform-specific code or dependencies are used.
The code generator can be customized by modifying:
XsdCodeGenerator.cs- Main generation logicCommandLineOptions.cs- Command-line options and modes- Generation modes for different output styles
Future enhancements may include:
- Configuration files for code generation settings
- Templates for customizing output format
- Plugins for extending generation logic
This tool is not included in the main solution file (Cql-Sdk.slnf) as it's a development tool, not part of the SDK itself. It should be run manually when XSD schemas are updated.
Important: Do not add this project to solution filters used for CI/CD builds.
If you see errors like "Undefined complexType", ensure you're passing all related schema files together:
# Wrong - missing dependencies
xsd2cs /c library.xsd
# Correct - all schemas together
xsd2cs /c library.xsd expression.xsd clinicalexpression.xsd cqlannotations.xsdBy default, the output file is named after the first schema file. Use /o: to specify a different output directory.
The tool includes comprehensive unit tests validating all features:
XsdToCSharpConverterTests (tools/XsdToCSharpConverterTests/):
- Round-trip JSON serialization/deserialization tests
- Tests loading from LibrarySets/Demo/Elm directory
- Validates default value initialization
ElmSerializerTests (Cql/CoreTests/):
- 13 comprehensive tests covering all quirks and features
- DefaultValueAttribute with constructor initialization
- *Specified pattern behavior
- XmlElementAttribute for array elements
- XmlIncludeAttribute for polymorphic types
- Abstract type generation
- Type ordering with XmlRootAttribute
- Tool identification (GeneratedCodeAttribute)
This project is licensed under the BSD 3-Clause license. See the LICENSE file in the repository root for details.