Search code examples
c#.netxmlxsd

Generate C# class from XML


Can I generate a C# class from an XML file?


Solution

  • Yes, by using xsd.exe

    D:\temp>xsd test.xml
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 4.0.30319.1]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file 'D:\temp\test.xsd'.
    
    D:\temp>xsd test.xsd /classes
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 4.0.30319.1]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file 'D:\temp\test.cs'.
    

    Notes

    Answer how to change directory in Developer Command Prompt to d:\temp may be useful.

    If you generate classes for multi-dimensional array, there is a bug in XSD.exe generator, but there are workarounds.