Comparison of XSD Code Generators [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm doing some research in code generation from xsd schema files.
My requirements:
Must generate C# 2.0 code (or above), using generic collections where needed.
Must generate comments from the xsd comments
Must generate fully serializable code.
Should be able to generate resuable basetypes when generating from multiple xsd's with the same includes.
(see also my other questions: How can I generate multiple classes from xsd’s with common includes? and How can I generate comments from xs:documentation tags in a wsdl?
I have found the following options:
Use xsd.exe (supplied with the SDK and Visual Studio)
XSDCodeGen from Daniel Cazzulino
Xsd2Code
CodeXS
XsdObjectGen by Microsoft
XSDClassGen (Seems to be missing in action)
Did I miss any? Because (1), (2) and (5) do not generate 2.0 code, and I have problems with serializing code from (3). What do you use when generating code?

I believe XSD2Code is the best tool currently available (in 2011).
I recently went through the same process at work of analysing the available tools out there so i thought i would provide an updated answer that relates to VS2010.
Our main driver was that xsd.exe does not generate XML doc from the XSD annotations, which we wanted as we have hundreds of type definitions. I tried all the tools listed above as well as others and most were either deprecated, unmaintained or unable to match the current functionality of xsd.exe available in VS2010.
Xsd2Code however is a superb tool and seems to be actively maintained. It provides all the functionality that was listed above and a lot more - the CodePlex page also has great examples of how the various options affect output.
It also has tight VS integration, including context menu integration and a custom build tool (which means that if you reference the XSDs in your project and specify the custom tool, it will automatically update the code as you update the XSD). All in all saved us a lot of work.
A quick summary of the other tools i looked at:
Dingo - Seems to be more aligned to Java
XSDCodeGen - More of a demo on how to write a custom build tool
CodeXS - Quite a good tool, but less integration, features and no longer maintained
XSDObjectGen - No longer maintained, less functionality than current xsd.exe
XSDClassGen - Could not locate it
OXM Library - Recommend looking at this project, maintained and great functionality
LINQ to XSD - Very cool project, but not what i was looking for
Addendum:
If you do decided to go ahead with XSD2Code, there are a number of issues i found working with the command-line tool. In particular, there are some bugs with the argument processing that require some arguments to be in a certain order as well as some undocumented dependencies (eg - automatic parameters & .NET version are order specific and dependent). The following are the steps i used to generate the code using XSD2Code and then cleanup the output - take the bits that apply to you as necessary:
Run the following batch file to generate the initial code, changing the paths to the correct locations:
#echo off
set XsdPath=C:\schemas
set OutPath=%XsdPath%\Code
set ExePath=C:\Progra~1\Xsd2Code
set Namespace=InsertNamespaceHere
echo.Starting processing XSD files ...
for /f %%a IN ('dir %XsdPath%\*.xsd /a-d /b /s') do call:ProcessXsd %%a
echo.Finished processing XSD files ...
echo.&pause&
goto:eof
:ProcessXsd
%ExePath%\Xsd2Code %~1 %Namespace% %XsdPath%\Code\%~n1%.cs /pl Net35 /if- /dc /sc /eit
echo.Processed %~n1
goto:eof
Perform the following steps to tidy up the generated code, as necessary:
Regex replace - current project, case, whole word - [System.Runtime.Serialization.DataContractAttribute(Name:b=:b:q,:bNamespace:b=:b*{:q})] with [DataContract(Namespace = \1)]**
Replace - current project, case, whole word - [System.Runtime.Serialization.DataMemberAttribute()] with [DataMember]
Regex replace - current project, case, whole word - System.Nullable<{:w}> with \1?
Regex replace - open documents, case, whole word - {:w}TYPE with \1
Replace - open documents, case, whole word - System.DateTime with DateTime, then add missing using statements
Replace - open documents, case, whole word - [System.Xml.Serialization.XmlIgnoreAttribute()] with [XmlIgnore]
Replace - current project - System.Xml.Serialization.XmlArrayAttribute with XmlArray
Replace - current project - System.Xml.Serialization.XmlArrayItemAttribute with XmlArrayItem
Regex replace - current project - ,[:Wh]+/// <remarks/> with ,

I have not yet checked this out, but Linq2XSD might be a useful alternative.
I'm going to give this one a shot. LINQ with XSD generation would be better than any of these tools you mentioned - provided it works nicely.

OpenSource project XSD to Classes worked perfect for me.

I a project a bit over a year ago we used CodeXS. With some minor adjustments (a script that cleaned up the generated code a bit) it worked a charm.
There is also Dingo, which have some very good extensibility features (which we didn't need).

Try OxmLibrary - http://oxmlibrary.codeplex.com

The best XSD class generator I've found is
thinktecture WSCF.blue
.
It's nicer than most of the others for two reasons:
Fixes naming. That means casing and plurization of types and property names.
Creates a separate file for each class.
Or, if you're looking for a T4 solution, you can try
XsdClassGen
.
This one isn't working for me. But the good news is that it's a T4 file, so it's easy to fix!

Here is web based example of using XSLT to transform XML to C# code.
The example takes a model (XML) that describes services (basically service names, namespaces, and list of operations), it then generates WCF services (interfaces, messages, faults, tests, etc) all in C#.net.

Related

Generate C# 9 classes from an XML schema file (xsd), including modern features (nullables mostly)

I have to implement support for ISO20022 file format in our application and the "xsd.exe" tool provided with Visual Studio 2022 is frustratingly inadequate.
The most frustrating point is the lake of proper support for nullable types in the generated code: when a XML element is marked as "optional", the generated code should properly generate a nullable member in the target class. Instead, it generates a mess of two fields (MemberName and MemberNameSpecified}.
This makes the code working with the result way less readable than it should and more error prone.
Another sore point is the missing support for related schemas: various versions of the ISO2022 XML schema files exists and financial entities typically do not support them all. Since the xsd.exe tool does not (to my knowledge) allow mappinh of multiple XSD and namespaces into a single C# namespace, identical elements in the spefifications generates identical objects but in different C# namespace. This force us to cut and paste identical code for identical objects that are in different namespaces.
Is there a way to get XSD.exe to generate a "modern" class from an XML shema?
Is there another tool that could fit the bill (paid tools are ok if they get the job done).
There are 2 really good open source and free options for C#/.NET (including Core + Framework):
https://github.com/mamift/LinqToXsdCore
Back in 2008, Microsoft did work on improving XSd.exe, and they shipped a successor tool called LinqToXsd, which improves upon XSD.exe in many ways. It has been ported over to .NET Core and does support multiple namespaces (importing + including), and adding #nullable enable annotations to the generated code. This tool produces .NET Standard 2 code that is very different from XSD.exe, and tries to model the schema more closely than XSD.exe. See some sample code here.
https://github.com/mganss/XmlSchemaClassGenerator
This project is newer, and has better support for #nullables through the AllowNullAttribute and MaybeNullAttribute instead of just the #nullable pragma. This project can also generate interfaces from element and attribute groups, which is nifty when you need it, which neither XSD.exe nor LinqToXsdCore supports. This tool also generates code that looks closer to what XSD.exe gives you.
You might want to fiddle with both of them before making a decision. But I recommend using XmlSchemaClassGenerator, unless you have legacy code based on the old .NET Framework version of LinqToXsd, then LinqToXsdCore is more compatible as it is a straight-forward port.

C#: namespaces and classes structure

Coming from Java , I'm used to the package structure (com.domain.appname.tier)
Now I've started working on a C# project , where all the projects have depth of 1:
i.e
ProjectA
- Utilities.cs
- Validation.cs
- ....
- Extraction.cs
and all the cs files are around 2,500 lines long ...
How do you order your classes and namespaces in C# so it will make sense , and keep the source file in logical size ?
The same way as I'd imagine you do in Java:
A few (< 10?) classes in each namespace, with namespaces arranged in a hierarchy
One class per source file
One or two screenfuls of text per source file
The project you've joined doesn't sound very structured and isn't a good example of good source code organisation.
In a similar way in Java, you just need to make some effort :) Some C# developers, especially with VB background, tend to write looooong classes and put them at the top level.
I would suggest reading Microsoft guidelines on the subject:
Design Guidelines for Developing Class Libraries
In particular you should look at the following section:
Guidelines for Names
Even if you are not writing a class library you may still benefit a lot from these guidelines. FxCop (or Code Analysis as it is named now) will flag many constructs that are not in accordance with these guidelines.
I would first start grouping the classes together into areas of functionality, areas around authorisation for example would go under a folder within a project.
Then update the namespaces of the classes in the folder to reflect the change, Resharper does this for you and newer versions of VS will probably do too.
Lastly (if you are able) I would start to break the classes to smaller more manageable size.
Here's an example of how I organize my solutions, which mirrors the namespace structure.
The project has a default namespace which, in this case, is CompanyName.ProjectName
Source files are organized logically into a directory structure. In the example, my WF4 activity designers are organized under Activities in a folder called Designers.
The way VS works is that, as you create directories in a project, you are also creating namespaces. So, if I were to add a new activity designer called "Foo" in the shown directory, its namespace would be
"CompanyName.ProjectName.Activities.Designers"
Visual studio takes the default namespace, then uses the folder structure to determine the namespace for a particular file. Of course, once the file is created, and you move a file, it isn't automatically refactored. But the system works very well for not only controlling namespaces for classes, but also for keeping files organized.
The same way as you would in Java.
In Java, packages organize classes in physical directories. I'm not sure about this, but the compiler even encourages this convention IIRC. In C# you're not obliged to organize your classes into separate directories that match your namespaces, but it's a very common convention though.
Speaking of namespaces in C#, they do not follow the com.domain.appname.tier convention, but use the Company.Product.Tier format.
How to reorganize large classes depends on the application. This is an exercise in applying OOP guidelines and applies to both Java and C#.
if you are deeply engaged in the project ,i recommend investing some time in redesinging the stucture the way you used to in java ,considering that packages are equivalent to namespaces in c#.

Learning Mono Source Code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I am interested in contributing something to mono whether it is a documentation or what ever. As a first step, I downloaded the source tree for going through the code. However, I thought if some one would've spend enough time to understand the project structure that would help everyone here. Any one point me out where the project structure is well explained?
NOTE: This is not a duplicate of question https://stackoverflow.com/questions/1655090/mono-source-code-walkthrough-tutorial, the answer to this question doesn't suffice my expectation.
You should have checked out (subversion checkout URLs here):
trunk/libgdiplus
This is a library used by System.Drawing.
trunk/mono
This is what we call the Mono runtime. Contains mainly C source code. Under this directory you can find:
data/: a few configuration files for different version (1.x, 2.x,...).
msvc*/: Visual Studio solution files to build the Mono runtime.
libgc/: the Boehm Garbage Collector sources.
mono/: Mono runtime sources.
mini/: JIT source code
metadata/: these are almost all the functions used by the Mono runtime (marshaling, thread pool, socket I/O, file I/O, console I/O, application domains, GC, performance counters,...). It's more or less one C file each.
util: miscellaneous functions.
io-layer/: Win32 I/O emulation functions.
trunk/mcs
This is where the C# compiler, the class libraries, class libraries tests and other tools are.
class/ : One folder per assembly. Each of them contains the source code for each assembly split in directories with the namespace name (ie, System/System.Configuration and so on) and usually a Test directory too. The only naming exception is mscorlib whose corresponding folder is called corlib.
For example, if you want to see the source code for System.Net.HttpWebRequest, which is in the System.dll assembly, you go to trunk/mcs/class/System/System.Net and there shoould be a file named HttpWebRequest.cs containing the code you're looking for.
mcs/: the sources for the C# compilers (mcs, gmcs, smcs, dmcs...)
tools/: these are a bunch of tools used for development (sn, wsdl,...), documentation (monodoc), etc. Most of the tools names match the MS ones.
There are a lot more directories around, but those are where you should look for the C and C# code. Also, I suggested trunk for the checkout, since you will get the most up-to-date sources that way.
Update: Mono resides now in github and mcs has been integrated into the mono repository.
Gonzalo provided a good overview of the different modules.
Since you also mentioned wanting to contribute to documentation, you'll want a few more pieces of information.
First, Documentation is stored in XML files within mcs/class/[assembly]/Documentation/, e.g. mcs/class/corlib/Documentation. The intent is to support multiple human languages (though only English is currently being worked on), so within Documentation is a language directory, usually en. Within en there are ns-*.xml files, e.g. mcs/class/corlib/Documentation/en/ns-System.xml contains documentation for the System namespace. Also within en are "dotted namespace" directories, and within those are XML files, one per type, for example mcs/class/corlib/Documentation/en/System.Collections.Generic/IEnumerable`1.xml.
This is also outlined within the mdoc(5) documentation, in the FILE/DIRECTORY STRUCTURE section.
Once you've found the documentation, you need to know the XML format, which is also described in the mdoc(5) documentation, in the NamespaceName/TypeName.xml File Format section. The XML dialect used is a variant of the ECMA 335 XML documentation, changed to have one file per type (instead of all types within a single monolithic file). This is also a superset of C# XML documentation (see Annex E. Documentation Comments, page 487).
Finally, there's the question of adding new types/members to the mcs/class/[assembly]/Documentation directory. If you have Mono built, you can use the doc-update Makefile target. This will run the appropriate assembly through mdoc(1) and update the appropriate files within the Documentation directory.
If you have any other documentation questions, don't hesitate to ask on the mono-docs-list mailing list.

Using reflection for code gen?

I'm writing a console tool to generate some C# code for objects in a class library. The best/easiest way I can actual generate the code is to use reflection after the library has been built. It works great, but this seems like a haphazard approch at best. Since the generated code will be compiled with the library, after making a change I'll need to build the solution twice to get the final result, etc. Some of these issues could be mitigated with a build script, but it still feels like a bit too much of a hack to me.
My question is, are there any high-level best practices for this sort of thing?
Its pretty unclear what you are doing, but what does seem clear is that you have some base line code, and based on some its properties, you want to generate more code.
So the key issue here are, given the base line code, how do you extract interesting properties, and how do you generate code from those properties?
Reflection is a way to extract properties of code running (well, at least loaded) into the same execution enviroment as the reflection user code. The problem with reflection is it only provides a very limited set of properties, typically lists of classes, methods, or perhaps names of arguments. IF all the code generation you want to do can be done with just that, well, then reflection seems just fine. But if you want more detailed properties about the code, reflection won't cut it.
In fact, the only artifact from which truly arbitrary code properties can be extracted is the the source code as a character string (how else could you answer, is the number of characters between the add operator and T in middle of the variable name is a prime number?). As a practical matter, properties you can get from character strings are generally not very helpful (see the example I just gave :).
The compiler guys have spent the last 60 years figuring out how to extract interesting program properties and you'd be a complete idiot to ignore what they've learned in that half century.
They have settled on a number of relatively standard "compiler data structures": abstract syntax trees (ASTs), symbol tables (STs), control flow graphs (CFGs), data flow facts (DFFs), program triples, ponter analyses, etc.
If you want to analyze or generate code, your best bet is to process it first into such standard compiler data structures and then do the job. If you have ASTs, you can answer all kinds of question about what operators and operands are used. If you have STs, you can answer questions about where-defined, where-visible and what-type. If you have CFGs, you can answer questions about "this-before-that", "what conditions does statement X depend upon". If you have DFFs, you can determine which assignments affect the actions at a point in the code. Reflection will never provide this IMHO, because it will always be limited to what the runtime system developers are willing to keep around when running a program. (Maybe someday they'll keep all the compiler data structures around, but then it won't be reflection; it will just finally be compiler support).
Now, after you have determined the properties of interest, what do you do for code generation? Here the compiler guys have been so focused on generation of machine code that they don't offer standard answers. The guys that do are the program transformation community (http://en.wikipedia.org/wiki/Program_transformation). Here the idea is to keep at least one representation of your program as ASTs, and to provide special support for matching source code syntax (by constructing pattern-match ASTs from the code fragments of interest), and provide "rewrite" rules that say in effect, "when you see this pattern, then replace it by that pattern under this condition".
By connecting the condition to various property-extracting mechanisms from the compiler guys, you get relatively easy way to say what you want backed up by that 50 years of experience. Such program transformation systems have the ability to read in source code,
carry out analysis and transformations, and generally to regenerate code after transformation.
For your code generation task, you'd read in the base line code into ASTs, apply analyses to determine properties of interesting, use transformations to generate new ASTs, and then spit out the answer.
For such a system to be useful, it also has to be able to parse and prettyprint a wide variety of source code langauges, so that folks other than C# lovers can also have the benefits of code analysis and generation.
These ideas are all reified in the
DMS Software Reengineering Toolkit. DMS handles C, C++, C#, Java, COBOL, JavaScript, PHP, Verilog, ... and a lot of other langauges.
(I'm the architect of DMS, so I have a rather biased view. YMMV).
Have you considered using T4 templates for performing the code generation? It looks like it's getting much more publicity and attention now and more support in VS2010.
This tutorial seems database centric but it may give you some pointers: http://www.olegsych.com/2008/09/t4-tutorial-creatating-your-first-code-generator/ in addition there was a recent Hanselminutes on T4 here: http://www.hanselminutes.com/default.aspx?showID=170.
Edit: Another great place is the T4 tag here on StackOverflow: https://stackoverflow.com/questions/tagged/t4
EDIT: (By asker, new developments)
As of VS2012, T4 now supports reflection over an active project in a single step. This means you can make a change to your code, and the compiled output of the T4 template will reflect the newest version, without requiring you to perform a second reflect/build step. With this capability, I'm marking this as the accepted answer.
You may wish to use CodeDom, so that you only have to build once.
First, I would read this CodeProject article to make sure there are not language-specific features you'd be unable to support without using Reflection.
From what I understand, you could use something like Common Compiler Infrastructure (http://ccimetadata.codeplex.com/) to programatically analyze your existing c# source.
This looks pretty involved to me though, and CCI apparently only has full support for C# language spec 2. A better strategy may be to streamline your existing method instead.
I'm not sure of the best way to do this, but you could do this
As a post-build step on your base dll, run the code generator
As another post-build step, run csc or msbuild to build the generated dll
Other things which depend on the generated dll will also need to depend on the base dll, so the build order remains correct

Is there an automatic code formatter for C#? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
In my work I deal mostly with C# code nowadays, with a sprinkle of java from time to time. What I absolutely love about Eclipse (and I know people using it daily love it even more) is a sophisticated code formatter, able to mould code into any coding standard one might imagine. Is there such a tool for C#? Visual Studio code formatting (Crtl+K, Crtl+D) is subpar and StyleCop only checks the source without fixing it.
My dream tool would run from console (for easy inclusion in automated builds or pre-commit hooks and for execution on Linux + Mono), have text-file based configuration easy to store in a project repository and a graphical rule editor with preview - just like the Eclipse Code Formatter does.
For Visual Studio, take a look at ReSharper. It's an awesome tool and a definite must-have. Versions after 4.0 have the code formatting and clean-up feature that you are looking for. There's also plugin integration with StyleCop, including formatting settings file.
You'll probably want Agent Smith plugin as well, for spell-checking the identifiers and comments. ReSharper supports per-solution formatting setting files, which can be checked into version control system and shared by the whole team. The keyboard shortcut for code cleanup is Ctrl + E, C.
In 'vanilla' Visual Studio, the current file can be automatically formatted with Ctrl + K, Ctrl + D, and Ctrl + K, Ctrl + F formats the selected text.
As for a runs-everywhere command line tool to be used with commit hooks, try NArrange. It's free, can process whole directories at once and runs on Mono as well as on Microsoft .NET.
Some people also use the Artistic Style command line tool, although it requires Perl and works better with C/C++ code than with C#.
The .NET Foundation just released their code formatting tool on GitHub
https://github.com/dotnet/codeformatter
It uses the Roslyn compiler services to parse project documents and convert them to their expected formatting conventions. They're applying this to some of the very old CLR code in order to make all the projects consistent.
Further to #Chris Karcher's answer - you can also automatically format the whole document by hitting Ctrl+K, Ctrl+D.
These formatting features work on a variety of file formats - it works wonders on ugly HTML.
Another option: NArrange;
free
console based (so good for commit hooks etc, but can still be used as an "External Tool" in VS)
flexible config file
For me, Ctrl + Shift + F maps to Find in Files. When I need to format code, I highlight it and hit Ctrl + K, Ctrl + F.
I understand this doesn't really address automated formatting. I just wanted to clarify for those who may not know this feature even exists in Visual Studio.
I've heard only good things about ReSharper. It's on my to-learn list.
http://www.sourceformat.com/
This tool is around (~30$). I tried it and it works nice (with multiple languages).
I like this tool the best because it doesn't check code file for correctness. I can post code snippets from the Internet and it will translate them correctly no matter if they are in missing parts of the code. Other tools I try complain in that cases. The tool can also be integrated easily into editors as it allows command line driving.
Other tools:
http://www.polystyle.com/index.jsp
http://astyle.sourceforge.net/ (open source)
Not directly, but I use the Agent Smith plugin for R# to do this. Unfortunately, R# isn't free.
Also take a look at Microsoft StyleCop
See this previous question:
Is there any tool for reformatting C# code?
Searching for [c#] astyle shows up some more previous questions too.
I haven't tried this (found it through Google). It might work. http://www.semdesigns.com/Products/Formatters/CSharpFormatter.html. It's fairly cheap at USD50, but a trial is not available.
Maybe you could be interested in this free Addin for Visual Studio 2010/2012.
Here is an open source code formatting tool which has amazing features
CodeMaid
If you want to do it online, have a freecodeformat.

Categories

Resources