Getting error during object instantiation for xsd generated dataset class

0

I've been supplied an XML file and an appropriate XSD file. I use the the xsd.exe program to generate a dataset class based on the XSD file (xsd /dataset TestV7.xsd), which I import into my VS2019 (Community Edition) C# project. When I try to instantiate the object using NewDataSet testset = new NewDataSet; I get the error

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>

Can anyone help me resolve this error? I'm pretty new when it comes to XML processing, but I can generally work things out. Here is the subset of the XSD file that gives me the error when compiled.

<?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>

I've been researching different things online and the XSD file looks to be valid, and xsd.exe doesn't give any errors when building the c# file. I don't know if it's something about the XSD file, but the code to generate the error in VS is pretty basic.

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();

    }
  }
}
c#
xml
xsd
visual-studio-2019
asked on Stack Overflow Nov 22, 2019 by Strudo76

2 Answers

0

The offending line appears to be the maxLength definition for IDType:

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

I don't know why it is not working, though, as this appears to be a valid xs:simpleType definition. Maybe you could make do without the maxLength, since you have the xs:pattern facets.

answered on Stack Overflow Nov 22, 2019 by ioadim • edited Nov 24, 2019 by ioadim
0

This is the code you should be using :

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

}
answered on Stack Overflow Nov 25, 2019 by jdweng

User contributions licensed under CC BY-SA 3.0