序列化XML时出错

时间:2011-09-28 16:25:35

标签: c# xml-serialization

您好,我在序列化为XML时遇到此错误

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

有人可以帮我解决最新情况吗? 根据类别:

/// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("CarrierAppointment", typeof(ProducerChannel[]), IsNullable=false)]
    [System.Xml.Serialization.XmlArrayItemAttribute("DistributionChannelInfo", typeof(ProducerChannel), IsNullable=false, NestingLevel=1)]
    public ProducerChannel[][][] Producer
    {
        get
        {
            return this.producerField;
        }
        set
        {
            this.producerField = value;
        }
    }

用法:

var producer = new InitialPurchaseOrder.ProducerChannel{DistributionChannelName = division};
        bdParty.Producer = new InitialPurchaseOrder.ProducerChannel[][][]// {{},{},{producer}};
                                    {new InitialPurchaseOrder.ProducerChannel[][]
                                        {new InitialPurchaseOrder.ProducerChannel[]{producer}}};

错误行:

var serializer = new System.Xml.Serialization.XmlSerializer(txLife.GetType());

相关XSD:

<xs:element name="Policy" minOccurs="0" maxOccurs="unbounded">

                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="Annuity" minOccurs="0" maxOccurs="unbounded">
                          <xs:complexType>
                            <xs:sequence>
                              <xs:element name="QualPlanType" minOccurs="0" maxOccurs="unbounded">
                                <xs:complexType>
                                  <xs:attribute name="tc" form="unqualified" type="xs:string" />
                                </xs:complexType>
                              </xs:element>
                            </xs:sequence>
                          </xs:complexType>
                        </xs:element>
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>

请告诉我如何解决这个问题。

谢谢,

2 个答案:

答案 0 :(得分:1)

听起来像xsd.exe中的一个错误我现在偶然发现了几次,它无法正确处理数组。

如果是这种情况,您必须修改生成的类,以便XmlSerializer可以使用它。 Producer属性的属性或类型不正确。

如果您已发布XML架构的相关部分,我可以肯定地说出来,但请尝试将Producer属性的类型从ProducerChannel[][][]更改为ProducerChannel[][]

如果您可以修改XML模式,那么this link可以提供帮助,而无需在生成后修改C#类。

答案 1 :(得分:0)

我认为错误信息非常清楚。您拥有ProducerChannel[][](或ProducerChannel[][][])属性,但您的XmlArrayItem属性正尝试将其序列化为ProducerChannel[]

相关问题