简体   繁体   English

XSD.exe和“循环组引用”

[英]XSD.exe and “Circular Group references”

I am attempting to build some classes so that I can deserialise an XML file created by a third party application. 我正在尝试构建一些类,以便我可以反序列化由第三方应用程序创建的XML文件。 Luckily the developer of the 3rd party application included a schema file with their code so that the XML file can be understood. 幸运的是,第三方应用程序的开发人员包含一个包含其代码的模式文件,以便可以理解XML文件。

When I use the XSD.exe tool from Visual Studio the process fails reporting the following error 当我使用Visual Studio中的XSD.exe工具时,该过程无法报告以下错误

"Group 'SegGroupOrSegmentGrouping' from targetNamespace='' has invalid definition: Circular group reference." “来自targetNamespace =''的组'SegGroupOrSegmentGrouping'具有无效定义:循环组引用。”

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 可以在此处找到模式文件的副本: 模式文件

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. 我从第三方公司获得了一个Schema,它从Web服务返回一个xml结构。 I then wanted to deserialise the response and store the information into a database with NHibernate. 然后我想反序列化响应并将信息存储到NHibernate的数据库中。

No problem I thought I'll just use xsd.exe and I'll have my classes. 没问题我以为我只会使用xsd.exe而且我会上课。 Unfortunately this was not to be. 不幸的是,这不是。 Xsd.exe failed with exactly the same error you are getting. Xsd.exe失败,出现了完全相同的错误。 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. 这里的答案是编写自己的C#类并使用适当的属性装饰它们。

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. 我花了大约3天的时间来编写课程结构(它很大)但我最终得到了一个高质量的解决方案。

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. 有一件事是肯定你将无法使用xsd.exe和谷歌搜索后尝试的大多数其他工具要么无法正常工作,要么是有缺陷的。

Try using svcutil ; 尝试使用svcutil ; it can handle the circular references. 它可以处理循环引用。

In the following example, eExact-Schema.xsd is an XSD that xsd.exe cannot handle. 在以下示例中, eExact-Schema.xsdxsd.exe无法处理的XSD。

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. 您现在可以使用此类并根据您的风格/需求进行更改,添加注释等,这将为您节省大量时间/搜索从头开始。

After trying several third party tools, I found that Liquid Technologies has a very robust generator called Liquid XML Data Binder 2012 . 在尝试了几个第三方工具之后,我发现Liquid Technologies有一个非常强大的生成器,名为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. 它可以从2.0开始为几乎任何版本的.net生成代码。 The classes it generates do depend on a redistributable dll that they provide. 它生成的类确实依赖于它们提供的可再发行的dll。 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. 对我来说最简单的方法是使用XSD.EXE从实际的XML文件创建XSD文件。 Then create a class from the new XSD file. 然后从新的XSD文件创建一个类。 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!!!! 如果引入了原始XML中不存在的节点或类型,您可能需要定期修改该类,但您将节省自己的编码时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM