无法生成临时类(结果=1)。错误 CS0030:无法转换类型“Type[]”要“输入”?
使用 xsd.exe 工具从 xsd 文件创建类后,出现此错误。于是我在网上搜索并找到了解决方案。这是链接:http://satov.blogspot.com/ 2006/12/xsdexe- generated-classes-causing.html
问题是这使得代码运行,但不知何故反序列化的数据似乎已损坏。我按照网站的建议做了,最后第二个数组维度始终为空(请参阅网站的评论,有人也遇到了这个问题)。问题是,现在我该如何解决这个问题?还有其他工具可以创建 xsd 文件吗?我尝试了 Xsd2Code,但没有成功。
谢谢 :-)
I get this error after I created a class from my xsd file using the xsd.exe tool. So I searched the net and found a solution. Here is the link: http://satov.blogspot.com/2006/12/xsdexe-generated-classes-causing.html
Problem is that this makes the code run, but somehow the deserialized data seems corrupt. I did what the site suggests and in the end the 2nd array dimension is always empty (see the comments of the site, somebody also had this problem). Question is, how do I solve this issue now? Is there another tool to create the xsd file? I tried Xsd2Code, without success.
Thanks :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您需要更改序列化类中成员变量的类型。例如,如果它引发如下错误:
我对生成的文件中的数据类型名称进行了搜索,发现了这一点:
将
Data[][]
替换为Data[]
- 将数据类型从二维数组更改为一维数组。它会解决你的问题。 :)You need to change the type of a member variable in the serialized class. For example if its raising an error like:
I ran a search on the Data type name in the generated file, and I found this:
Replace
Data[][]
withData[]
- Change the type of Data from a 2D array to a 1D array. It would solve your problem. :)有同样的问题,但 Xsd2Code 没有与 VS2012 集成。因此,我转到 xsd.exe 生成的 .cs 文件并执行以下操作:
Find [][]
[] 。
替换有效的
Had the same problem, but Xsd2Code didn't integrate with VS2012. So instead I went to my xsd.exe generated .cs file and did:
Find [][]
Replace []
which worked.
我收到此错误。在您的解决方案中,该文件中有一个 reference.cs 文件,您需要搜索“[][]”,然后其中会有两个结果。
在您需要从“中删除一个“[]”之后[][]”来自两个地方..
它对我有用..
谢谢..
I got this error.In your solution there is reference.cs file in that file you need to search "[][]" and then there will be two results in it..
After you need to remove one "[]" from "[][]" from both places..
It works for me..
Thanks..
添加
每次
之后
和
<代码>
架构文件中的元素如果您不想松动数组的维度。
Add
<xs:attribute name="tmp" type="xs:string" />
after every
<xs:sequence maxOccurs="unbounded">
<xs:element ../>
</xs:sequence>
and
<xs:sequence>
<xs:element maxOccurs="unbounded"/>
</xs:sequence>
element in your schema file if you don't want to loose dimension of the array.
对我来说,它有助于修补用于生成代码的 XML。它发生在以下情况:
然后由 xsd 优化为双数组名称条目
我所做的是:
xsd 不会优化它,而是保留单个数组名称
For me it helps to patch the XML used to generate the code. It happens when:
then this is optimized by xsd to double array name entry
What I did is:
the xsd doesn't optimize it but leaves the single array name
如果它在 VB.net 中,那么你必须在 Reference.vb 中搜索 ()() 并替换为 ()
If its in VB.net then you got to search for ()() in your Reference.vb and replace with()
对于我的情况,问题是由于 XmlArrayItem property 属性的声明无效造成的。
从
我更改为适当的类型:从字符串到 ClassName
希望这有帮助!
For my case, the issue cases due to an invalid declaration for XmlArrayItem property attribute.
From
I changed with appropriate type: from string to ClassName
Hope this helps!
这确实是 @WaldemarGałęzinowski 的答案 https://stackoverflow.com/a/35896991/157224
扩大了一点。
xsd.exe
具有一项优化,当序列中有一个没有属性的无界元素时,该优化就会启动。优化将避免为父级创建特殊类型,而是使其成为子级数组。
ChildType[] 父级 { get;放; }
而不是ParentType Parent { get;放; }
并且您可以像Parent[0]
而不是Parent.Child[0]
那样访问子级。(我发现这种优化有时有点令人困惑)
这里发生的事情是你多了一层无界、无属性的元素
优化应用两次,结果为
GrandChildType[][] 父级 {get; set;}
并且您可以访问您最喜欢的第一个孙子,例如Parent[0][0]
而不是Parent.Child[0].GrandChild[ 0]
。问题是.Net序列化器不支持数组数组并生成无效代码。
我不知道为什么微软这些年来没有修复这个错误,但解决方法很简单。
只需通过向序列添加可选属性或可选元素来强制 xsd.exe 为父级或子级生成类。例如
导致
Parent[0].GrandChild[0]
This is really @WaldemarGałęzinowski's answer https://stackoverflow.com/a/35896991/157224
expanded a bit.
xsd.exe
has an optimization that kicks in when you have a single unbounded element without attribute in a sequence.The optimization will avoid creating a special type for the parent and instead make it an array of children.
ChildType[] Parent { get; set; }
instead ofParentType Parent { get; set; }
and you access the children likeParent[0]
instead ofParent.Child[0]
.(I find this optimization a bit confusing sometimes)
What is happening here is you have one more level of unbounded, attribute-less element
The optimization is applied twice and the result is
GrandChildType[][] Parent {get; set;}
and you access yourfavoritefirst grandchild likeParent[0][0]
instead ofParent.Child[0].GrandChild[0]
.The problem is the .Net serializer does not support arrays of arrays and generates invalid code.
I have no idea why Microsoft has not fixed this bug in all these years but the workaround is simple.
Just force
xsd.exe
to generate a class for the parent or child by adding an optional attribute or an optional element to the sequence. e.g.Which leads to
Parent[0].GrandChild[0]