Generate an object model from an XML string - ISerializable - Roy Osherove's Blog

Generate an object model from an XML string

Tommy sent me an email:

"I've written a little web application that could come in handy for your many projects. It generates the C# code needed to read and write the given xml (Just past some xml in the textbox). You can find it @ http://xgeneration.tuyware.com/
It's candidate for a lot of improvements, but it's already usefull.
Enjoy it !"

I tried it out with an RSS feed and it generated classes for all the elements in the feed, with the root element being the one you'd start from, calling root.ReadXML().

Very nice! I can see how this could be useful in many scenarios, but be sure not to paste in anything secure, since this is a web application!

Published Tuesday, March 04, 2008 10:05 AM by RoyOsherove

Comments

Tuesday, March 04, 2008 11:04 AM by Ran Davidovitz

# re: Generate an object model from an XML string

very nice,

I didn't understand why he didn't use Iterator for the Enumerator

Tuesday, March 04, 2008 9:06 PM by Kyralessa

# re: Generate an object model from an XML string

Useful, I guess, for people who refuse to use VB, since reading XML is pretty darned easy in VB 2008.

Tuesday, March 04, 2008 11:45 PM by NP

# re: Generate an object model from an XML string

Nice please provide more examples in your site.

I donot know if you have time but some features nice to have would be:

How to generate a class with

i. properties,

ii. methods,

iii. inheritance/specific base classes

iv. specific access modifiers such as public/private etc...

v. from a DataTable or a dataset.

Wednesday, March 05, 2008 3:53 AM by Igor Brejc

# re: Generate an object model from an XML string

Any chance of releasing this as a standalone tool?

Wednesday, March 05, 2008 3:54 AM by Tommy Uytterhaegen

# re: Generate an object model from an XML string

The project started as 'MeWare'. That's the reason why no decent webpage is present and why it has certain loose ends.

As soon as I find some time I'll create a webpage with some more information about how to use it and I'll also update the webapp to cover more cases and to make the generated code 'better'.

Keep your eye on the page :)

Wednesday, March 05, 2008 5:11 AM by Tommy Uytterhaegen

# re: Generate an object model from an XML string

As for the request for a stand alone tool : The code was structured in a way so that it will be possible, I already have some mockup code that does just this, with some extra stuff ofcourse.

For example: The webapp will try to guess what type is found (Int, String, ...). The mockup I have now let's you change this if you want it to be something else. It's not unthinkable that I will finish this tool and release it. If I do I will definatly mention it on the webapp page.

Wednesday, March 05, 2008 6:41 AM by Poul

# re: Generate an object model from an XML string

I don't see how that tool is much different from the xsd.exe that has always been with the .net framework - just call it twice:

xsd test.xml --> leads to a .xsd file

xsd -classes test.xsd --> leads to a .cs file

After that it's just plain old xml-serialization....

Friday, March 07, 2008 7:00 AM by Poul Foged

# re: Generate an object model from an XML string

Just realized - xsd.exe is part of the visualstudio sdk - anyways...