2

What tools have you used to create class source code from xml files? Is this an edge case that I need to roll my own? I have need to create DTOs from some XML files, but the XML files are subject to change (add/remove attributes) so I need to be able to quickly update them.

I'm reallly not impressed with the .xml -> .xsd -> bloated .cs approach, and was looking for something to hopefully generate simple POCOs for me. Are there any tools you've used or seen that do this?

Nate
  • 30,286
  • 23
  • 113
  • 184
  • Are you referring to using `xsd.exe` (http://msdn.microsoft.com/en-us/library/x6c1kb0s%28VS.80%29.aspx) as being bloated in your question? – Russ Cam Jun 04 '10 at 21:35

4 Answers4

6

There is a built-in way to do this in VS2008 and later, T4. Hanselman has a bunch of great links in one place.

blowdart
  • 55,577
  • 12
  • 114
  • 149
  • 1
    T4 reminds me sooo much of Apache Velocity... there's a .NET port called NVelocity by the way. T4 is much better integrated to VS, than NVelocity, so probably worth looking into. Also nice to see that it's not restricted to sheerly .cs, but supports any other text format... – code4life Jun 04 '10 at 21:39
  • Is there a way I can use an existing class from within the T4 template? I'm getting an error "The type or namespace name 'myclass' could not be found." I have added it as a reference to the project the T4 template is in, am I doning something wrong? – Nate Jun 04 '10 at 22:22
  • Nope, you have to start by writing the code to do the transformation, because it's a general engine, not just for xml and xslt – blowdart Jun 04 '10 at 23:06
  • No, I have a T4 style for loop that generates the code I want on a hard coded list, I have a class that can return a list of data from my XML file, I Just can't figure out how to initialize my class in the T4 template. I've seen other people use framework base classes syetem.io.file for instance. – Nate Jun 06 '10 at 16:01
  • I ended up needing the `<#@ assembly name="PathToDll">` tag at the top of my file and it works great! – Nate Jun 07 '10 at 18:57
2

How about Xsd2Code: http://xsd2code.codeplex.com/

code4life
  • 15,655
  • 7
  • 50
  • 82
0

I realise that this is a rather old post and you have probably moved on. But I had the same problem as you so I decided to write my own program.

It is in no way elegant but it did the job for me.

You can get it here: Please make suggestions if you like it.

SimpleXmlToCode

Talon
  • 3,466
  • 3
  • 32
  • 47
0

T4 sounds perfect for this.

It's essentially an ASP.NET like syntax to generate code based on your template.

You would write the template to output the code for the POCO as needed, and then embed code in the template to iterate over your XML collection.

T4 is part of Visual Studio 2008 (but undocumented), and 2010. The Microsoft DSL tools gives support for T4 for Visual Studio 2005 as a separate download.

http://msdn.microsoft.com/en-us/library/bb126445.aspx

Kilanash
  • 4,479
  • 1
  • 14
  • 11