C# XSD Schema Tools and Code Generation¶
Date: December 19, 2025 Topic: Linux tools for generating C# classes from XSD schemas
Overview¶
This conversation explored the available tools and methods for generating C# classes from XSD (XML Schema Definition) files on Linux systems, including both third-party tools and custom solutions using .NET's built-in libraries.
Available Tools for Linux¶
Third-Party Cross-Platform Tools¶
XmlSchemaClassGenerator (dotnet-xmlschemagen)
- The most popular and reliable cross-platform option
- Can be installed as a .NET global tool
- Installation: dotnet tool install -g dotnet-xmlschemagen
- Basic usage: dotnet-xmlschemagen --file schema.xsd --namespace MyCompany.Models --output ./Generated/
- Generates XmlSerializer-compatible C# classes
- Actively maintained open-source project
Microsoft xsd.exe - Traditional Windows tool, not natively available on Linux - Was not initially ported to .NET Core - Can potentially be run through Wine on Linux - Limited cross-platform support
Custom Solution Using System.Xml.Schema¶
The conversation included a detailed custom implementation showing how to programmatically generate C# classes from XSD schemas using .NET's built-in System.Xml.Schema namespace.
System.Xml.Schema Approach¶
Key Classes and Components¶
Core Classes:
- XmlSchemaSet - Manages and compiles multiple XSD schemas
- XmlSchema - Represents an individual XSD schema document
- XmlSchemaElement - Represents XML elements defined in the schema
- XmlSchemaComplexType - Represents complex types with child elements and attributes
- XmlSchemaParticle - Base class for schema particles (sequences, choices, elements)
- XmlSchemaAttribute - Represents XML attributes in the schema
Implementation Features¶
The custom implementation created during the conversation includes:
- Schema Loading and Validation
- Loads XSD files using
XmlSchemaSet - Compiles and validates schema structure
-
Provides error handling for invalid schemas
-
Class Generation
- Processes complex types and converts them to C# classes
- Handles sequences, choices, and nested elements
- Generates properties with appropriate XML serialization attributes
-
Converts XML naming conventions to C# PascalCase conventions
-
Property Type Mapping
- Maps XSD simple types to C# types (string, int, DateTime, etc.)
- Handles complex types as nested classes
-
Supports collections (List
) for elements with maxOccurs > 1 or unbounded -
XML Serialization Attributes
- Adds
[XmlRoot]attributes to classes - Adds
[XmlElement]attributes to element properties - Adds
[XmlAttribute]attributes to attribute properties - Ensures generated classes are compatible with System.Xml.Serialization
Usage Example¶
# Create new console project
mkdir XsdToCs
cd XsdToCs
dotnet new console
# Run the generator
dotnet run path/to/schema.xsd MyNamespace output.cs
Generated Code Structure¶
The custom generator produces C# code with: - Appropriate using statements - Namespace declaration - Classes with XML serialization attributes - Properties with get/set accessors - List initialization for collection properties
Comparison: Third-Party vs Custom Solution¶
When to Use Third-Party Tools (XmlSchemaClassGenerator)¶
- Quick setup and immediate use
- Production-ready with extensive testing
- Handles edge cases and complex schemas
- Regular updates and community support
- Comprehensive XSD feature support
When to Use Custom System.Xml.Schema Solution¶
- Need full control over code generation
- Specific customization requirements
- Learning purposes or educational projects
- Integration into existing code generation pipelines
- Minimal dependencies preferred
Limitations of Basic Custom Implementation¶
The example custom implementation is educational and would need enhancements for production use: - Limited support for advanced XSD features (unions, restrictions, patterns) - Basic type mapping without full XSD type system support - No support for schema imports and includes - Limited namespace handling for multi-schema scenarios - No support for inheritance and extension mechanisms - Basic error handling and validation
Recommendations¶
For Most Use Cases: Use XmlSchemaClassGenerator (dotnet-xmlschemagen) as it's the most mature, well-tested, and feature-complete solution for Linux.
For Learning or Custom Requirements: The System.Xml.Schema approach provides a solid foundation for understanding how XSD parsing works and can be extended to meet specific needs.
For Simple Schemas: Either approach works well, with the custom solution being lighter if you only need basic functionality.
Additional Resources¶
- .NET XML Schema documentation: System.Xml.Schema namespace
- XmlSchemaClassGenerator GitHub repository for the most popular tool
- XmlSerializer documentation for understanding the serialization attributes
- XSD specification for understanding schema structure