XSD.exe and "Circular Group references" - c#

I am attempting to build some classes so that I can deserialise an XML file created by a third party application. Luckily the developer of the 3rd party application included a schema file with their code so that the XML file can be understood.
When I use the XSD.exe tool from Visual Studio the process fails reporting the following error
"Group 'SegGroupOrSegmentGrouping' from targetNamespace='' has invalid definition: Circular group reference."
Any help in how I can generate the class files in light of this error would be appreciated.
A copy of the schema file can be found here : schema file

Try using svcutil; it can handle the circular references.
In the following example, eExact-Schema.xsd is an XSD that xsd.exe cannot handle.
Example:
C:\SRC\Exact>svcutil eExact-Schema.xsd /language:C# /dataContractOnly /importxmltypes /out:exact.cs
This is always a good place to start; you can now use this class and alter to suit your style/needs, add comments, etc, and it will save you a lot of time/searching over doing it all from scratch.

I had this same problem recently,
I was given a Schema from a third party company who were returning an xml structure from a webservice. I then wanted to deserialise the response and store the information into a database with NHibernate.
No problem I thought I'll just use xsd.exe and I'll have my classes. Unfortunately this was not to be. Xsd.exe failed with exactly the same error you are getting. This is because it is unable to resolve circular references.
I spent a good few days looking at alternatives until in the end I wrote my own class structure to the schema and was able to deserialise perfectly. The answer here is to write your own C# classes and decorate them with the appropriate attributes.
Save yourself some time and heartache and don't continue to try and generate the classes you need automatically in the end although time consuming the classes you write won't make the compromises that most tools (which don't work perfectly) will make you make.
Took me about 3 days to write the class structure (it was large) but I ended up with a quality solution.
One thing is certain you will not be able to use xsd.exe and most other tools I tried after googling this either did not work properly or were buggy.

After trying several third party tools, I found that Liquid Technologies has a very robust generator called Liquid XML Data Binder 2012. It was able to handle the circular group reference problem I faced. It can generate code for just about any version of .net from 2.0 on. The classes it generates do depend on a redistributable dll that they provide. I'm using the trial version and I wouldn't be surprised if a purchase of the full version will be necessary before I go to release. However, having saved me probably a hundred hours or more of error prone hand coding, I can't complain.

The easiest method for me is to create the XSD file from the actual XML file with XSD.EXE. Then create a class from the new XSD file. You may be required to modify the class periodically if nodes or types are introduced that did not exist in the original XML but you will save yourself HOURS of coding time!!!!

Related

How can I merge XML comments back into a .cs file?

Extracting comments out of C# class files is relatively easy (see Extracting doc comments from C# source file), but I've recently come into the opposite problem.
My project has a bunch of classes that are generated from XML schema (via Microsoft's xsd.exe). I'd like to write out XML documentation on these classes, but we have to recreate them every so often. I'd like to be able to write out the comments, extract them into their own .xml file, run xsd.exe to recreate the classes from the schema, and then merge the comments back in.
Is there some way to do this?
I peeked inside xsd.exe of the v8.1A version of the SDK (it was written in .NET so you can use a tool like JustDecompile to see what it does). The method which drives the XSD->C# generation is called ImportSchemasAsClasses. What it does is read the XSD files into a raw XmlSchemas instance, import the schemas with the XmlSchemaImporter class (into an abstract model called type mapping), then generate code with the XmlCodeExporter class into Code DOM classes. As you can see from the links, these are public, but undocumented API's. But you can easily replicate what xsd.exe does (you only need to call public .NET API's, just copy and paste what xsd.exe does) and tap into the right place to add some comments into the Code DOM, nice and clean, no string manipulations needed.

Create Intellisense from stored dynamic objects

Im about to create some settings for MVC projects and sites, based on dynamic variables etc.
These settings will be stored in xml for easy read and write.
My question now, after reading about extending the intellisense in this question:
Is it possible to provide intellisense for dynamic objects in visual studio?
Is if its possible to read my saved settings (which are stored at runtime) and then for the next run build a intellisense from that?
I.E. for each of these site.setings.layout.width a list of the "older" saved xml-defined defined dynamics will be able to show up?
If all you need is xml "intellisense" then consider designing xml schemas and dump them in Visual Studio installation Folder\xml\Schemas or include them in your solution and VS will do the rest if the namespaces match appropriately.
Edit:
Coming back to this after a while. No other answer appears to have been given so I'll try to be more creative.
Visual Studio has an option to generate an xsd from an xml file. Note that the schema will be mostly an approximation but it will match the file and will be a good description of structure. If you could find a way to call that from a command line (or possibly find a similar tool for the step) you can then chain that with xsd.exe and generate C# classes from it at build time (prebuild step)
If point one is too cumbersome you could try to write a T4 template that reads a previous configuration file and generates your custom code based on that. Generating a POCO property structure based on some xml should be fairly simple with T4. The template should be run as a precompiled step.
Note that both suggestions involve static code generation. A full dynamic solution could be done with F# type providers but that is not available for C#.

Create MULTIPLE class files based on an XSD

I may be attempting something that is not possible with the XSD tool but I wanted to ask before moving on to a simpler solution.
I have an XSD file that has multiple elements (and multiple complex types) that will generate multiple classes in one code file (I do not like this). For the sake of having clean and readable class files generated from the XSD tool, I would like for each element to be placed in a seperate code file, not all placed in one code file as partial classes.
Does anyone know how to do this? Or is my only solution for this breaking the XSD into one schema for each of the xml elements in the schema?
The MSDN article http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=VS.100).aspx does not provide language that specifies whether or not this can be done.
Thanks in advance for any answers or comments.
This doesn't answer your question directly, but I wanted to throw a couple things out:
I generally find it counter-productive to separate generated code. I always like to generally follow the "one class per file" rule, but I make an exception here, because I often deal with very large schemas. Even in their own directory, I don't want to have to diff tens (to hundreds) of files when I generate a new version of the code. I find it very convenient to have all the generated code diffable in one file.
Now, to offer a possible solution - Resharper has the ability to pull all the classes out of a file and put them in their own files. If you right click the file in the solution explorer, you can say Refactor → Move types into matching files.... Of course, this isn't anywhere near as convenient as just generating it this way, but I don't know of a tool that will do that.
The problem I'm trying to solve was either in separate class files or one singular, but unique classes rather than the same namespace containing multiple classes. As a result, I was looking for a similar answer and found this reference to share Getting xsd.exe To Not Create Duplicate Classes
This is a simple fix, but it took me a long time to figure out. You have to use xsd.exe to compile all of your classes at once, so rather than running two separate commands, you just need to run one:
C:\Solution\Project>xsd.exe Types.xsd Request.xsd Response.xsd /c
Now you have one file, Response.xsd, with all three classes in it.

Mapping XML with Schema to Entity in C#

I am trying to find an article I read about a month ago. I the article the author talk about using the XML classes in .net to map xml with a schema to POCO objects and also showed how to create a schema straight from the objects that he had. This is exactly what I'm trying to do know, but all I seem to be able to find is articles talking about manually walking through the xml and extracting the values for your objects.
I know I can manually spin through the XML to get my objects, but I have several different objects I need to map, so I'd rather go down the automated route if there is one. Does anyone know how to build a schema from a .net object and then map XML files to it with some .net automagic?
The xsd.exe command-line utility is your friend!
See the MSDN docs for details on how to use it and what you can do with it.
In brief:
It can take a .NET assembly and generate XSD schema for each of the classes, or it can take an existing XML file and generate a XSD schema from it (as good as it can guess it), and then a C# or VB.NET class from that schema that allows you to easily deserialize a XML file into a .NET object directly.
There's also Xsd2Code - a Visual Studio (at least 2008 - not sure about 2010) plugin that allows you to have a XSD file in your project, and create a C# class from it in the Solution Explorer.

Read XML into xsd.exe generated classes. Good idea?

I have a fairly complex XML coming my way and I have the XSD for it. I generated classes via xsd.exe and read XML into the class structure via the XmlSerializer described here.
It works great. However, this is the first time I've done it this way and I'll be reading in tons of XML files going forward from various sources. How reliable is this method? Could one say with certainty that if the XML file conforms to the XSD specification, that the XmlSerializer will be able to read it in just fine?
Short answer: it's better. This is exactly how MSfts web services work, so if what you described didn't work, any of the .NET consumers would fail, like when you add a reference to a web service in .NET or Silverlight.

Categories