【问题标题】:Getting error during object instantiation for xsd generated dataset class在 xsd 生成的数据集类的对象实例化期间出现错误
【发布时间】:2019-11-22 05:43:35
【问题描述】:

我得到了一个 XML 文件和一个适当的 XSD 文件。我使用 xsd.exe 程序生成基于 XSD 文件 (xsd /dataset TestV7.xsd) 的数据集类,我将其导入到我的 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();

    }
  }
}

【问题讨论】:

  • 是什么让您认为可以使用 DataSet ReadXml() 方法读取 xml 文件?这是行不通的。当下降数很大(超过4级)时读取xml方法会将数据分成无法重组的表。数据集的创建如下:1)根标签数据集名称2)子标签是表名3)Grand Children标签是列名4)Great Grandchildren作为行数据。获得4级标签后,Net库会将数据分成附加表,无法重新组合。
  • 在我尝试读取 XML 之前就发生了错误。只是数据集的声明会导致错误。
  • 您使用的哪个库有 NewDataSet?它不是网络的一部分。你从哪里复制代码?
  • NewDataSet 是 XSD.exe 程序在针对提供的 xsd 文件运行时生成的数据集
  • 它不使用命令:xsd.exe -c -l:cs 它生成要与 c# xml 序列化程序一起使用的类。您一定使用了错误的选项。

标签: c# xml xsd visual-studio-2019


【解决方案1】:

这是您应该使用的代码:

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

}

【讨论】:

  • 我想我可以解决这个问题。看来我还有很多东西要学……
  • 如果我需要提出与此相关的另一个问题,我是提出一个新问题、编辑我原来的问题还是在评论中提问? xsd 程序指定了一个对我来说没有意义的枚举类。我最初发布的摘录中没有包含 xsd 的那部分。
  • 这三个中的任何一个都可以。我监控所有的 xml c# 帖子。
  • 我把它作为一个新问题来做,因为我自己找不到答案。 stackoverflow.com/questions/59131641/…
【解决方案2】:

违规行似乎是 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 方面。

【讨论】:

  • 注:xsd.exe /classes TestV7.xsd 似乎不会导致同样的问题。显然不是你要找的东西,但我想我应该提一下。
  • 我对此进行的所有研究似乎都表明没有无效的语法或结构。我可能没有 maxLength 节点,因为数据将由生成 XSD 文件的同一个地方提供,我希望他们遵守自己的规范。我希望不要编辑 XSD 文件,而只是“按原样”使用它,但如果我可以在这里和那里进行一点小改动,我想这就足够了。
  • 对于这个特定的模式,最大最小长度似乎对 IDType 没有影响,因为内容已经受到两个 xs:patterns 的限制,它们指定了一个数字字符串 5 或 8字符(因此,不小于 5 也不多于 8 是有效的)。但是,我同意您对修改第三方提供的 xml 架构的担忧,我也认为这在语法上是正确的。
  • 我自己有一个部分解决方案。我从提供的 xml 文件生成了一个全新的 xsd 文件,并使用该 xsd 文件通过 xsd.exe 程序生成数据集代码。我使用的 xml 文件应该包含所有数据元素,因为它是一个初始加载文件,因此生成的数据集应该包含它们发送的任何数据。
猜你喜欢
  • 2011-12-16
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 2013-06-23
  • 2021-07-30
相关资源
最近更新 更多