C# Class (source code) Generator from XML file - c#

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?

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

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

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

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

Related

Generate class from XSD - compact solution

I have XSD file, which seems rather complex (I am very new to working with XSD).
My task is to create a program, which would generate XML files based on the XSD schema (in a more detail - we will get a CSV file with the data and these need to be serialized into a XML). I did a research and tried various techniques of generating C# class from the XSD file, where the most 'compact' was xsd2code plugin for Visual Studio.
Nonetheless, this plugin has generated over 7,000 lines of code which quite shocked me as it was just one giant mess (for me).
My question now is - is there a better way (or maybe some switch I forgot to check) which will generate rather compact C# class? If not, then what is the next step that people have to do once they get C# class? Do they have to additional manual post processing so that the file is more 'programmer-friendly', or ...?
Thank you for your guidance; any help or tip will be highly appreciated!

Template engine for rendering Text Files c#

I am in the process of writing a class library that will generate an edi file. I am wanting to write a template for the file so that i can pass in some form of a model and generate the file (similar to how ASP MVC functions).
I have looked at t4 files as well as xslt and i have concerns on both. t4 looks to have an interesting way of approaching related classes, and file locks during compile. xslt is possible, but i was hoping there might be something out there that was a bit more intuitive.
I have tried searching to see if this is a duplicate issue, but if it is, please let me know, and my appologies.
Much appreciated.

Get all mandatory fields in a xsd schema file

Working against an xsd and I need to give a list of all mandatory fields to a client
Is there a way i can quickly do in c#? or use a free tool?
File is quite big I would like to avoid to do it manually
thanks
you could try xsd2code which is a codeplex project. See here This makes classes within your code based on the Xsd format.
This is a free tool integrated into your visual studio and might help you find the necessary fields. As well that it allows you to create code easily to the xsd schema.

C# How to generate code from code

Is it possible to generate and build some c# code based on the code from the same project. I tried with T4 and Reflection, but there are some assembly locking issues. Is there any other way?
Reflection works fine for me. You can get around assembly locking issues by isolating your build task to a separate AppDomain within VS. When the task completes, any assemblies you need to use for code generation will be unloaded together with the task's AppDomain. See AppDomainIsolatedTask.
You can definitely write your own code generator, all in C# - after all, "code" that's being generated is just a text file you write out.
But what's wrong with T4 templates? They offer a lot of functionality that you don't have to reinvent yet again - why not use it? Can you tell us in more detail what problems you're having with T4?
T4 is really just a bunch of classes in .NET, too - so you could definitely write your own code generator handling some of the logic, and use T4 to do the templating & replacing those template values part. But again: in order to help you diagnose your T4 problems, we'd need to know more about those....
This example from Oleg Sych uses FXCop's introspection engine instead of reflection. That way, the assemblies do not get locked.
Unfortunately, Reflection is optimized
for code execution. One particular
limitation makes it ill-suited for
code generation - an assembly loaded
using Reflection can only be unloaded
with its AppDomain. Because T4
templates are compiled into .NET
assemblies and cached to improve
performance of code generation, using
Reflection to access the component
assembly causes T4 to lock it.
Alternatively, if you're only targeting Linq to SQL classes, you could generate code from the dbml file instead of the code that L2S generates from the dbml. I've got an example of something similar (an edmx file) on my own blog.
There is a third party C# .NET variant of JavaCC that we use at work.
Also an interesting article about how to make one:
http://msdn.microsoft.com/en-us/magazine/cc136756.aspx
It really depends on what exactly are you trying to achieve, but on a general case I'd recommend using T4 templates.
And yes, it is possible to use T4 templates inside your project to generate code in your project based on some local settings, but you should define what are you trying to do.
If you want to generate code based on some classes that you define in the same project - this doesn't sound like something easily achievable (after all you want to compile some of the classes in the current project, generate some code based on them and after that generate classes again... umm.. ?)
But if you want to store some settings and then run the T4 template and generate some code based on these settings - this is easily achievable. T4MVC is an example (they generate code based on a settings file that is copied and stored in the project alongside the T4 template). This template also looks at the current files available in the solution, and generates string constants based on each file. That kind of sounds like it would really help you with your problem, whatever it is :)
If you're still unsure - you can specify more details about what you want to do, and we'll try to help you :)

Localization with xml without compilation?

I am looking for the best way to implement a winform with different languages, but i don't want to use the resources files of Visual Studio because you always have to recompile.
I have found the following solution to use XML files without compilation:
http://www.codeproject.com/KB/miscctrl/xml_localization.aspx
I find it is OK, users can edit the xml and in the future they could provide my application with translations.
Do you know a better way for this?
Instead of defining your own XML-based internalization system you could go the standard way by using one such as Translation Memory eXchange format.

Categories

Resources