From raw xml (no schema) to c# class?

I have an xml file that I would like to generate a c# class for. Is there any quick and easy way to do this? I don't have a schema to go with my xml file, it's just raw xml. Any ideas?


13.10.2009 18:09:42
You can generate a schema using Microsoft's XSD Schema creator.
George Stocker 13.10.2009 18:11:48

All code generation tools that I know will require a schema - but you can easily create one from your XML data file.

You can use xsd.exe to infer a XML schema from the XML data file:

xsd.exe yourdata.xml 

This will create a yourdata.xsd. Of course, xsd.exe can only be guessing - pretty good sometimes, not so good other times. You might want to check (and possibly modify) the schema before proceeding.

(You can do the same in Visual Studio: load the XML file, and from the XML menu, choose "Create schema").

From that schema, you can then create serializable classes:

xsd.exe yourdata.xsd /classes

This will create a yourdata.cs file that contains a C# class that can be serialized into and deserialized from your XML data files.


13.10.2009 18:13:45