代码之家  ›  专栏  ›  技术社区  ›  Jim McKeeth

.NET xsd导入器创建不可序列化的类

  •  6
  • Jim McKeeth  · 技术社区  · 16 年前

    我正在使用.NET XSD.EXE 导入程序从XSD文件集合生成C#类。当我试图将其中一个类序列化为XML时,它失败了( ),当我深入研究时,我发现其中一个创建的类似乎是错误的。

    <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代码:

    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;
            }
        }
    }
    

    它使 Warnings 数组的数组 WarningType InvalidOperationException

    • 错误CS0030:无法将类型“WarningType[]”转换为“WarningType”
    • 错误CS0029:无法将类型“WarningType”隐式转换为“WarningType[]”
    • 错误CS0029:无法将类型“WarningType”隐式转换为“WarningType[]”

    但是如果我从 WarningType[][] WarningType[] 然后它就可以序列化了。

    我想要什么 是正确序列化为XML并根据XSD进行验证的C代码。现在这个锯齿状数组似乎是错误的,因为如果我删除它,它就会生成XML。

    1 回复  |  直到 16 年前
        1
  •  4
  •   Cheeso    16 年前

    XsdObjbectGen 工具,也来自Microsoft,但从.NET SDK独立和带外发布。


    事实上,在重读你的问题时,你从未说过你真正想要的。是否希望SuccessType包含数组数组?

    是吗 警告类型 ? 您提供的代码片段之间存在一些分歧。


    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。然后,我在该DLL上运行了xsd.exe,并生成了以下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,则会得到一个封装数组的类型。

    推荐文章