.NET XSD进口商造成不可序列化的类进口商、序列化、NET、XSD

2023-09-03 05:02:45 作者:淡笑

我使用的是.NET XSD.EXE 进口商从集合中生成C#类的XSD文件。当我试图序列化的一个类到XML失败( InvalidOperationException异常),当我挖到它,我发现它创建的类中的一个似乎是错误的。

I am using the .NET XSD.EXE importer to generate C# classes from a collection of XSD files. When I tried to serialize one of the classes to XML it failed (InvalidOperationException), and when I dug into it I discovered it one of the created classes appears to be wrong.

下面是相关的XSD code:

Here is the pertinent XSD code:

<xsd:complexType name="SuccessType">
    <xsd:annotation>
        <xsd:documentation>Indicates in a response message that a request was successfully processed.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
        <xsd:element ref="Warnings" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
</xsd:complexType>
<!-- .. snip .. -->
<xsd:element name="Warnings" type="WarningsType">
    <xsd:annotation>
        <xsd:documentation>The processing status of a business message and any related warnings or informational messages.</xsd:documentation>
    </xsd:annotation>
</xsd:element>
<!-- .. snip .. -->
<xsd:complexType name="WarningsType">
    <xsd:annotation>
        <xsd:documentation>A collection of warnings generated by the successful processing of a business message.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
        <xsd:element ref="Warning" maxOccurs="unbounded"/>
    </xsd:sequence>
</xsd:complexType>
<!-- .. snip .. -->
<xsd:element name="Warning" type="WarningType">
    <xsd:annotation>
        <xsd:documentation>Defines details of a warning that occurred during message processing.</xsd:documentation>
    </xsd:annotation>
</xsd:element>
<!-- .. snip .. -->
<xsd:complexType name="WarningType">
    <xsd:annotation>
        <xsd:documentation>Defines details of a warning that occurred during message processing.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
        <xsd:element ref="WarningCategory"/>
        <xsd:element ref="WarningCode"/>
        <xsd:element ref="WarningShortMessage"/>
        <xsd:element ref="WarningMessage"/>
    </xsd:sequence>
</xsd:complexType>

在这里,是从它生成的C#code:

And here is the C# code generated from it:

public partial class SuccessType
{

    private WarningType[][] warningsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("Warning", typeof(WarningType), IsNullable = false)]
    public WarningType[][] Warnings
    {
        get
        {
            return this.warningsField;
        }
        set
        {
            this.warningsField = value;
        }
    }
}

这让警告 WarningType 的数组的数组。当我试图序列化到XML,我得到一个 InvalidOperationException异常 异常:

It made Warnings an array of an array of WarningType. When I attempt to serialize that to XML I get an InvalidOperationException exception:

无法生成临时类(结果= 1)。 错误CS0030:无法将类型'WarningType []'到'WarningType 错误CS0030:无法将类型'WarningType []'到'WarningType 错误CS0029:无法隐式转换类型'WarningType'到'WarningType [] 错误CS0029:无法隐式转换类型'WarningType'到'WarningType []

但是,如果我改变产生code从 WarningType [] [] WarningType [] 然后它系列化的罚款。

But if I change the generated code from WarningType[][] to WarningType[] then it serializes fine.

短?这是一个错误的XSD.EXE或XSD文件不正确的?也许有在XmlSerializer的问题?

Short of editing the generated C# class whenever I regenerate this (which hopefully will be less frequently going forward), is there any other solution? Is this a bug in xsd.exe or is the XSD file incorrect? Maybe there is a problem in the XmlSerializer?

我要什么是C#code是正确的序列化为XML的验证对XSD。眼下交错数组似乎是错误的,因为如果我删除它,然后它生成的XML。

What I want is C# code that correctly serializes to XML that validates against the XSD. Right now the jagged array seems to be wrong because if I remove it then it generates the XML.

我在使用Visual Studio 2008。

I am using Visual Studio 2008.

推荐答案

有在xsd.exe工具的错误。我不记得这个特殊的一个,但我记得发现的问题与交错数组,并有可能这仍然是一个错误。如果你愿意,你可以使用 XsdObjbectGen 工具,同样来自微软,而是独立出来的带外发布的.NET SDK。

There are bugs in the xsd.exe tool. I don't remember this particular one, but I do remember finding problems with jagged arrays, and it's possible this remains a bug. if you're willing, you could use the XsdObjbectGen tool, also from Microsoft, but released independently and out-of-band from the .NET SDK.

有一件事情你可以做的是相反的方向:写C#code,然后生成与XSD.EXE模式,看看有什么不同。这是可能XSD.EXE想要的模式寻找一个特定的方式,以便正确地生成正确的$ C $下交错数组。

One thing you could do is go the reverse direction: write the C# code, then generate the schema with xsd.exe, and see what is different. It's possible xsd.exe wants the schema to look a particular way, in order to correctly generate correct code for jagged arrays.

其实,在重新阅读你的问题,你从来没有说过你真正想要的。你想SuccessType包含一个数组,数组,还是没有?

Actually, upon re-reading your question, you never said what you really wanted. Do you want SuccessType to contain an array-of-arrays, or not?

和它是 WarningsType 或 WarningType ?有间剪您提供的code一些不同的意见。

And is it WarningsType or WarningType? There's some disagreement between the code snips you provided.

假设你想要的阵列的阵列,我写了这个C#code:

Assuming you wanted the array-of-arrays, I wrote this C# code:

public class WarningType
{
    public String oof;
}


public partial class SuccessType
{
    private WarningType[][] warningsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("Warning", typeof(WarningType[]), IsNullable = false)]
    public WarningType[][] Warnings
    {
        get
        {
            return this.warningsField;
        }
        set
        {
            this.warningsField = value;
        }
    }
}

...然后编译成一个DLL。然后我跑XSD.EXE在该DLL,并生成此XSD:

... then compiled it into a DLL. Then I ran xsd.exe on that DLL, and generated this XSD:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="WarningType" nillable="true" type="WarningType" />
  <xs:complexType name="WarningType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="oof" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="SuccessType" nillable="true" type="SuccessType" />
  <xs:complexType name="SuccessType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Warnings" type="ArrayOfArrayOfWarningType" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ArrayOfArrayOfWarningType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="Warning" type="ArrayOfWarningType" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ArrayOfWarningType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="WarningType" nillable="true" type="WarningType" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

...它往返。如果我再在该模式运行XSD.EXE,我得到一个包装一个数组的阵列类型。

...and it round-trips. If I then run xsd.exe on that schema, I get a type that wraps an array-of-arrays.