views:

133

answers:

3

I have this XML file, and I want to deserialize it to an object. But I don't want to type its class definition. There is any tool that can create the C# code of the class for me, inferring the data types from sample data?

+5  A: 

Yes. Out of the box, you can use xsd.exe to generate XSD files from XML. You can also use this tool to generate classes from XSD files.

The code it produces is limited, which is why there are some third party tools that have stepped in.

Two of those tools include LiquidXML (costs money) and CodeXS (free). We use CodeXS, because it is free and extensible. We have extended it quite a bit.

EDIT: CodeXS has an online tool. Just give it an XSD. It produces your classes for you. They also have a command-line tool (source code) which is extensible and doesn't require you to send the XSD to their web service. We use it as a pre-build step.

Brian Genisio
++ thank you!!!!!
Jason Watts
Fixed the links. Seems the URL parser on SO is not perfect and you need to URL-encode the GET parameter that is passed.
0xA3
I used CodeXS online tool. I found a bug, when you upload the ZIP file, it generated just a log, then I used the URL method and it worked.
Jader Dias
I used also xsd.exe, and it required some DTD files. Thank god the XML provider also provided them, and I could generate the XSD.
Jader Dias
I found another bug: the generated class had a property of the type someclass[][] instead of someclass[]. But I could correct it manually. Probably CodeXS' fault.
Jader Dias
check my following question, please : http://stackoverflow.com/questions/1009154/
Jader Dias
+1  A: 

Check out LINQ-to-XSD

It requires that you write a schema for your XML but then it's pretty good about a direct translation to objects.

Stephan
A: 

Liquid Technologies has a good tool for this purpose (Data binding) http://www.liquid-technologies.com/. You'll really need to define a schema though instead of letting such a tool "infer" it from sample data.

One of the benefits of Liquid that we've found is that it can also generate code for Java, C++, C#, VBA etc. All very consistent.

Alan Moore