XSD.exe and “Circular Group references”

前端 未结 4 997
暖寄归人
暖寄归人 2020-12-16 12:11

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

相关标签:
4条回答
  • 2020-12-16 12:47

    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.

    0 讨论(0)
  • 2020-12-16 12:50

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

    0 讨论(0)
  • 2020-12-16 12:58

    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.

    0 讨论(0)
  • 2020-12-16 13:14

    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.

    0 讨论(0)
提交回复
热议问题