无法生成临时类(结果=1)。错误 CS0030:无法转换类型“Type[]”要“输入”?

发布于 2024-11-19 20:25:16 字数 393 浏览 1 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

讽刺将军 2024-11-26 20:25:16

您需要更改序列化类中成员变量的类型。例如,如果它引发如下错误:

无法生成临时类(结果=1)。
错误 CS0030:无法将类型“Data[]”转换为“Data”。

我对生成的文件中的数据类型名称进行了搜索,发现了这一点:

[System.Xml.Serialization.XmlArrayItemAttribute("Data", typeof(Data), IsNullable=false)]
public Data[][] Row

Data[][] 替换为 Data[] - 将数据类型从二维数组更改为一维数组。它会解决你的问题。 :)

You need to change the type of a member variable in the serialized class. For example if its raising an error like:

Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'Data[]' to 'Data'.

I ran a search on the Data type name in the generated file, and I found this:

[System.Xml.Serialization.XmlArrayItemAttribute("Data", typeof(Data), IsNullable=false)]
public Data[][] Row

Replace Data[][] with Data[] - Change the type of Data from a 2D array to a 1D array. It would solve your problem. :)

起风了 2024-11-26 20:25:16

有同样的问题,但 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.

み零 2024-11-26 20:25:16

我收到此错误。在您的解决方案中,该文件中有一个 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..

野却迷人 2024-11-26 20:25:16

添加
每次
之后




<代码>


架构文件中的元素如果您不想松动数组的维度

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.

温柔一刀 2024-11-26 20:25:16

对我来说,它有助于修补用于生成代码的 XML。它发生在以下情况:

<Names>
    <Name></Name>
    <Name></Name>
</Names>

然后由 xsd 优化为双数组名称条目

我所做的是:

<Names>
    <Dummy></Dummy>
    <Name></Name>
    <Name></Name>
</Names>

xsd 不会优化它,而是保留单个数组名称

For me it helps to patch the XML used to generate the code. It happens when:

<Names>
    <Name></Name>
    <Name></Name>
</Names>

then this is optimized by xsd to double array name entry

What I did is:

<Names>
    <Dummy></Dummy>
    <Name></Name>
    <Name></Name>
</Names>

the xsd doesn't optimize it but leaves the single array name

盗心人 2024-11-26 20:25:16

如果它在 VB.net 中,那么你必须在 Reference.vb 中搜索 ()() 并替换为 ()

If its in VB.net then you got to search for ()() in your Reference.vb and replace with()

水染的天色ゝ 2024-11-26 20:25:16

对于我的情况,问题是由于 XmlArrayItem property 属性的声明无效造成的。

[XmlArrayItem("MyArray", typeof(string))]
public List<ClassName> Items{ get; set; }

我更改为适当的类型:从字符串到 ClassName

[XmlArrayItem("MyArray", typeof(ClassName))]
public List<ClassName> Items{ get; set; }

希望这有帮助!

For my case, the issue cases due to an invalid declaration for XmlArrayItem property attribute.

From

[XmlArrayItem("MyArray", typeof(string))]
public List<ClassName> Items{ get; set; }

I changed with appropriate type: from string to ClassName

[XmlArrayItem("MyArray", typeof(ClassName))]
public List<ClassName> Items{ get; set; }

Hope this helps!

尹雨沫 2024-11-26 20:25:16

这确实是 @WaldemarGałęzinowski 的答案 https://stackoverflow.com/a/35896991/157224
扩大了一点。

xsd.exe 具有一项优化,当序列中有一个没有属性的无界元素时,该优化就会启动。

Parent- Child

优化将避免为父级创建特殊类型,而是使其成为子级数组。

ChildType[] 父级 { get;放; } 而不是 ParentType Parent { get;放; } 并且您可以像 Parent[0] 而不是 Parent.Child[0] 那样访问子级。
(我发现这种优化有时有点令人困惑)

这里发生的事情是你多了一层无界、无属性的元素

Parent-child-grandchild

优化应用两次,结果为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.

Parent-Child

The optimization will avoid creating a special type for the parent and instead make it an array of children.

ChildType[] Parent { get; set; } instead of ParentType Parent { get; set; } and you access the children like Parent[0] instead of Parent.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

Parent-child-grandchild

The optimization is applied twice and the result is GrandChildType[][] Parent {get; set;} and you access your favorite first grandchild like Parent[0][0] instead of Parent.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.
workaround

Which leads to Parent[0].GrandChild[0]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文