xsd 生成的数据集 class 的对象实例化期间出现错误

Getting error during object instantiation for xsd generated dataset class

我得到了一个 XML 文件和一个合适的 XSD 文件。我使用 xsd.exe 程序基于 XSD 文件 (xsd /dataset TestV7.xsd) 生成数据集 class,并将其导入到我的 VS2019(社区版)C# 项目中。当我尝试使用 NewDataSet testset = new NewDataSet; 实例化对象时,出现错误

System.ArgumentException
  HResult=0x80070057
  Message=Cannot set Column 'IDCode_text' property MaxLength. The Column is SimpleContent.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

谁能帮我解决这个错误?在 XML 处理方面,我是个新手,但我通常可以解决问题。这是 XSD 文件的子集,它在编译时给我错误。

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
     elementFormDefault="qualified"
     attributeFormDefault="unqualified">

     <xs:element name="HOST">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Header" type="Header" minOccurs="0"/>
                <xs:element name="Departments" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Department" type="Department" maxOccurs="unbounded"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="Type">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:length value="1"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="IDType">
        <xs:restriction base="xs:string">
            <xs:minLength value="5"/>
            <xs:maxLength value="8"/>
            <xs:pattern value="\d{5}"/>
            <xs:pattern value="\d{8}"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="HostAction">
        <xs:restriction base="xs:string">
            <xs:length value="1"/>
            <xs:enumeration value="D"/>
            <xs:enumeration value="I"/>
            <xs:enumeration value="U"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="BusinessPillar">
        <xs:restriction base="xs:string">
            <xs:length value="3"/>
            <xs:enumeration value="ALM"/>
            <xs:enumeration value="CCC"/>
            <xs:enumeration value="CSD"/>
            <xs:enumeration value="CWD"/>
            <xs:enumeration value="IGA"/>
            <xs:enumeration value="TAS"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="Header">
        <xs:sequence>
            <xs:element name="IDCode">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="IDType">
                            <xs:attribute name="Type" use="required">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:length value="1"/>
                                        <xs:enumeration value="C"/>
                                        <xs:enumeration value="I"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:attribute>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
            <xs:element name="CreationDate" type="xs:dateTime"/>
            <xs:element name="Version">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:length value="5"/>
                        <xs:pattern value="\d\.\d\.\d"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            <xs:element name="PricingZone" minOccurs="0">
                <xs:simpleType>
                    <xs:restriction base="xs:integer">
                        <xs:totalDigits value="2"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            <xs:element name="Business" type="BusinessPillar" minOccurs="0"/>
            <xs:element name="State">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:minLength value="2"/>
                        <xs:maxLength value="3"/>
                        <xs:enumeration value="ACT"/>
                        <xs:enumeration value="NSW"/>
                        <xs:enumeration value="NT"/>
                        <xs:enumeration value="NZ"/>
                        <xs:enumeration value="QLD"/>
                        <xs:enumeration value="SA"/>
                        <xs:enumeration value="TAS"/>
                        <xs:enumeration value="VIC"/>
                        <xs:enumeration value="WA"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Department">
        <xs:sequence>
            <xs:element name="Description" minOccurs="0">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:minLength value="1"/>
                        <xs:maxLength value="40"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="DepartmentNumber" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:length value="2"/>
                    <xs:pattern value="\d{2}"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="Action" type="HostAction" use="required"/>
    </xs:complexType>
</xs:schema>

我一直在网上研究不同的东西,XSD 文件看起来是有效的,并且 xsd.exe 在构建 c# 文件时没有给出任何错误。我不知道它是否与 XSD 文件有关,但在 VS 中生成错误的代码非常基础。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;

namespace ImportV7Host
{
  class Program
  {
    static void Main(string[] args)
    {
      // create a new dataset for the imported data
      NewDataSet testset = new NewDataSet();

      testset.Dispose();

    }
  }
}

违规行似乎是 IDType 的 maxLength 定义:

<xs:simpleType name="IDType">
        <xs:restriction base="xs:string">
            <xs:minLength value="5"/>
            <xs:maxLength value="8"/> <!-- Offending line -->
            <xs:pattern value="\d{5}"/>
            <xs:pattern value="\d{8}"/>
        </xs:restriction>
    </xs:simpleType>

不过,我不知道为什么它不起作用,因为这似乎是一个有效的 xs:simpleType 定义。也许你可以不用 maxLength,因为你有 xs:pattern 个面。

这是您应该使用的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);

            XmlSerializer serializer = new XmlSerializer(typeof(HOST));

            HOST host = (HOST)serializer.Deserialize(reader);
        }
    }
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:2.0.50727.6421
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------


    // 
    // This source code was auto-generated by xsd, Version=2.0.50727.3038.
    // 


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class HOST {

        private Header headerField;

        private Department[] departmentsField;

        private string typeField;

        /// <remarks/>
        public Header Header {
            get {
                return this.headerField;
            }
            set {
                this.headerField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
        public Department[] Departments {
            get {
                return this.departmentsField;
            }
            set {
                this.departmentsField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Type {
            get {
                return this.typeField;
            }
            set {
                this.typeField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class Header {

        private HeaderIDCode iDCodeField;

        private System.DateTime creationDateField;

        private string versionField;

        private string pricingZoneField;

        private BusinessPillar businessField;

        private bool businessFieldSpecified;

        private HeaderState stateField;

        /// <remarks/>
        public HeaderIDCode IDCode {
            get {
                return this.iDCodeField;
            }
            set {
                this.iDCodeField = value;
            }
        }

        /// <remarks/>
        public System.DateTime CreationDate {
            get {
                return this.creationDateField;
            }
            set {
                this.creationDateField = value;
            }
        }

        /// <remarks/>
        public string Version {
            get {
                return this.versionField;
            }
            set {
                this.versionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
        public string PricingZone {
            get {
                return this.pricingZoneField;
            }
            set {
                this.pricingZoneField = value;
            }
        }

        /// <remarks/>
        public BusinessPillar Business {
            get {
                return this.businessField;
            }
            set {
                this.businessField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool BusinessSpecified {
            get {
                return this.businessFieldSpecified;
            }
            set {
                this.businessFieldSpecified = value;
            }
        }

        /// <remarks/>
        public HeaderState State {
            get {
                return this.stateField;
            }
            set {
                this.stateField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class HeaderIDCode {

        private HeaderIDCodeType typeField;

        private string valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public HeaderIDCodeType Type {
            get {
                return this.typeField;
            }
            set {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value {
            get {
                return this.valueField;
            }
            set {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public enum HeaderIDCodeType {

        /// <remarks/>
        C,

        /// <remarks/>
        I,
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class Department {

        private string descriptionField;

        private string departmentNumberField;

        private HostAction actionField;

        /// <remarks/>
        public string Description {
            get {
                return this.descriptionField;
            }
            set {
                this.descriptionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string DepartmentNumber {
            get {
                return this.departmentNumberField;
            }
            set {
                this.departmentNumberField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public HostAction Action {
            get {
                return this.actionField;
            }
            set {
                this.actionField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    public enum HostAction {

        /// <remarks/>
        D,

        /// <remarks/>
        I,

        /// <remarks/>
        U,
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    public enum BusinessPillar {

        /// <remarks/>
        ALM,

        /// <remarks/>
        CCC,

        /// <remarks/>
        CSD,

        /// <remarks/>
        CWD,

        /// <remarks/>
        IGA,

        /// <remarks/>
        TAS,
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public enum HeaderState {

        /// <remarks/>
        ACT,

        /// <remarks/>
        NSW,

        /// <remarks/>
        NT,

        /// <remarks/>
        NZ,

        /// <remarks/>
        QLD,

        /// <remarks/>
        SA,

        /// <remarks/>
        TAS,

        /// <remarks/>
        VIC,

        /// <remarks/>
        WA,
    }

}