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

    }
  }
}
Strudo76
  • 15
  • 6
  • What makes you think you can read the xml file using the DataSet ReadXml() method? It is not going to work. The read xml method when the number of descents are to large (more than 4 level) will fragment the data into tables that cannot be recombined. The dataset is created as follows : 1) Root tag the dataset name 2) child tags are the table names 3) Grand Children tags are the column names 4) Great Grandchildren as the row data. After you get 4 levels of tags Net library will fragment the data into addition tables with no way of recombining. – jdweng Nov 22 '19 at 10:10
  • The error occurs before I even attempt to read the XML. Just the declaration of the dataset causes the error. – Strudo76 Nov 24 '19 at 08:14
  • What library are you using that has NewDataSet? It is not part of Net. Where did you copy the code from? – jdweng Nov 24 '19 at 11:21
  • The NewDataSet is the dataset generated by the XSD.exe program when run against the supplied xsd file – Strudo76 Nov 25 '19 at 02:52
  • It does not with the command : xsd.exe -c -l:cs It generates classes to be used with c# xml serializer. You must be using the wrong options. – jdweng Nov 25 '19 at 05:52
  • Looks like you are getting the exception due to the xml not matching the schema. You should not be editing supplied schema unless you know the root cause is the schema and not the xml file. Most cases the xml files are wrong and must be corrected. by the people who created the xml. – jdweng Nov 25 '19 at 05:59
  • I don't understand how the excepting is caused by the xml? The exception occurs without the xml even being referenced. The xsd and xml file both come from the same place. My goal was to use the xsd program to generate the dataset so I could read the xml file into a dataset for processing. I'll look at using your code below, as I haven't done much like this before, so I'm getting a little lost... – Strudo76 Nov 27 '19 at 21:31
  • I think NewDataSet is an inherited class and the exception is occurring in a custom constructor. If you right click on NewDataSet and then select definition you will find where the class is defined. – jdweng Nov 28 '19 at 02:59

2 Answers2

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.

ClmCpt
  • 421
  • 2
  • 8
  • N.B.: xsd.exe /classes TestV7.xsd does not seem to cause the same problem. Obviously not what you're looking for, but I thought I should mention it. – ClmCpt Nov 22 '19 at 08:15
  • All the research I did into this seems to indicate there is no invalid syntax or structures. I could possibly do without the maxLength node as the data will be supplied by the same place that produced the XSD file, and I'd hope that they would adhere to their own specification. I was hoping not to edit the XSD file and just use it 'as supplied', but if I can get to function with a small change here and there I guess it will be sufficient. – Strudo76 Nov 24 '19 at 08:17
  • For this specific schema, max-min length don't seem to make a difference for IDType, since the content is already restricted by the two xs:patterns, which specify a numeric string of 5 or 8 characters (so, neither less than 5, nor more than 8 would be valid). However, I share your concern about modifying an xml schema supplied by a third party, which I also think is syntactically correct. – ClmCpt Nov 24 '19 at 18:40
  • I have myself a partial solution. I generated a completely new xsd file from the supplied xml file, and used that xsd file to generate the dataset code using the xsd.exe program. The xml file I used should contain all data elements as it is an initial load file, so the resulting dataset should encompass any data they send. – Strudo76 Nov 25 '19 at 02:54
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,
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • I think I can get my head around this. I have a lot to learn it seems... – Strudo76 Nov 27 '19 at 23:37
  • If I need to ask another question relating to this, do I make a new question, edit my original question or just ask in the comment? The xsd program is specifying an enumerated class that doesn't make sense to me. That section of the xsd wasn't included in the extract I originally posted. – Strudo76 Nov 28 '19 at 01:23
  • Any of the 3 will work. I monitor all the xml c# postings. – jdweng Nov 28 '19 at 02:55
  • I did it as a new question since I've been unable to find an answer myself. https://stackoverflow.com/questions/59131641/xsd-exe-generating-unwanted-string-values-in-enum-type-instead-of-just-the-digit – Strudo76 Dec 02 '19 at 01:46