20

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

BENBUN Coder
  • 4,801
  • 7
  • 52
  • 89
  • Having done some further research it looks like there are a number of issues with the XSD.exe tool. I am making progress using the code generation tool of a third party tool which generated the C# code from the schema without any problems ( and generates much better class files as well ). – BENBUN Coder Mar 09 '10 at 09:22
  • What third party tool did you use, because I have the same problem? – jitm Jul 08 '10 at 15:29
  • BENBUN, could you tell us about the tool please? – Yuri Astrakhan Oct 25 '10 at 06:35
  • 2
    Yurik - I can't recall the tool we used now. As it happens we ran in to other problems using the generated code from that tool. In the end I wrote my own code by hand and it works fine. – BENBUN Coder Oct 26 '10 at 09:11

4 Answers4

15

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.

Jon
  • 9,156
  • 9
  • 56
  • 73
NKCSS
  • 2,716
  • 1
  • 22
  • 38
  • 2
    Insane. This created the whole huge bloat of classes from the OpenCollada specification, while XSD immediately failed. Nice! – Ray Aug 21 '15 at 23:05
  • Even more insane is that this solution is actually about the exact same xsd I was having trouble with. – Arno Peters Jan 09 '20 at 07:58
  • Exact for the win @Volkirith :) – NKCSS Jan 17 '20 at 15:06
  • 4
    It actually doesn't create a real C# model where fields are represent by plain c# properties. Instead, it creates classes with `XmlNode` arrays supposed to store properties. – Olivier Jacot-Descombes Feb 17 '20 at 20:34
  • this is absolutely useless if what you're looking for is actual structure in generated code. Svcutil does no data binding – chris Nov 15 '22 at 23:21
13

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.

krystan honour
  • 6,523
  • 3
  • 36
  • 63
  • 2
    Krystan - I have to agree, that was exactly my finding as well. I ended up writing the classes by hand. – BENBUN Coder Nov 12 '10 at 14:58
  • 1
    I just had the same problem, but solved it without manual coding, just by incremental execution of xsd.exe on modified xsd files. http://devio.wordpress.com/2012/05/22/dealing-with-circular-group-reference-errors-in-xsd-exe/ – devio May 22 '12 at 20:04
  • 1
    The incremental approach would work but its no good if you ever need to generate in an automated way and modifying the xsds is a total no-no if you aren't the owner. Just my $0.02 – krystan honour Dec 09 '13 at 09:16
  • 1
    +1 for manually writing classes. These tools work well as proofs of concept for simple examples. In real-life complex schemas they failed me every single time... – ddaniel Feb 25 '15 at 09:17
3

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.

Erikest
  • 4,997
  • 2
  • 25
  • 37
0

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!!!!

D Beck
  • 1
  • 1
  • Except when the XSD is complex, and there are NO XML files that would actually have all of the possible elements as defined in the XSD. – David V. Corbin Jul 26 '23 at 13:04