Missing the line grouping level in a table-like structure

Nov 26, 2012 at 1:16 PM

Hi all,

I have the following XSD (which describes a basic tables):

    <xs:complexType name="Column">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="id" use="optional" />
                <xs:attribute type="xs:string" name="value" use="optional" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="Header">
        <xs:sequence>
            <xs:element name="column" maxOccurs="unbounded" minOccurs="0" type="Column" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Line">
        <xs:sequence>
            <xs:element name="column" type="Column" maxOccurs="unbounded" minOccurs="0" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="BandRate">
        <xs:sequence>
            <xs:element name="header" type="Header"/>
            <xs:element name="line" maxOccurs="unbounded" minOccurs="0" type="Line" />
        </xs:sequence>
        <xs:attribute type="xs:string" name="type" use="optional" />
        <xs:attribute type="xs:byte" name="nbLines" use="optional" />
    </xs:complexType>

Which describes XML data such as the following examples.
<bandRate type="table">
    <header>
        <column id="c1" value="Band" />
        <column id="c2" value="Price" />
    </header>
    <line>
        <column id="c1" value="band 7 toll free" />
        <column id="c2" value="1.50" />
    </line>
    <line>
        <column id="c1" value="band 6 toll free" />
        <column id="c2" value="1.00" />
    </line>
</bandRate>

As you can see there are, in the above example, 2 lines and one header objects.

Xsd2Code generates the following C# object:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
    public partial class BandDiscount : EntityBase<BandDiscount>
    {

        private List<Column> headerField;

        private List<Column> lineField;

        private string typeField;

        private sbyte nbLinesField;

        private bool nbLinesFieldSpecified;

        public BandDiscount()
        {
            this.lineField = new List<Column>();
            this.headerField = new List<Column>();
        }

Question
I would have hoped to get 

        private List<Header> headerField;

        private List<Line> lineField;

So that I can first loop on the sequence of lines of the table and then, within each current line, loop on its columns. At the moment, I can only iterate on the lumped together list of columns.  In other words, all columns of all lines are lumped together under the lineField member.

Is there a way to achieve this result (generate the Line (and Header)  intermediate object)?

Dec 24, 2012 at 10:51 AM

Hello,

You can do it only if you re-define Header and Line with Colum type like this :

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="package" type="Column" />
    <xs:complexType name="HeaderCollection">
        <xs:sequence>
            <xs:element name="column" type="Header" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="LineCollection">
        <xs:sequence>
            <xs:element name="column" type="Line" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Column">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="id" type="xs:string" use="optional" />
                <xs:attribute name="value" type="xs:string" use="optional" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="BandRate">
        <xs:sequence>
            <xs:element name="header" type="HeaderCollection" />
            <xs:element name="lines" type="LineCollection" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="type" type="xs:string" use="optional" />
        <xs:attribute name="nbLines" type="xs:byte" use="optional" />
    </xs:complexType>
    <xs:complexType name="Header">
        <xs:complexContent>
            <xs:extension base="Column" />
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="Line">
        <xs:complexContent>
            <xs:extension base="Column" />
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

 


 

namespace ConsoleApplication2
{
    using System;
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System.Collections;
    using System.Xml.Schema;
    using System.ComponentModel;
    using System.Collections.Generic;


    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Line))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Header))]
    public partial class Column
    {

        public string id { get; set; }
        public string value { get; set; }
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value { get; set; }
    }

    public partial class Line : Column
    {
    }

    public partial class Header : Column
    {
    }

    public partial class HeaderCollection
    {

        public List<Header> column { get; set; }

        public HeaderCollection()
        {
            this.column = new List<Header>();
        }
    }

    public partial class LineCollection
    {

        public List<Line> column { get; set; }

        public LineCollection()
        {
            this.column = new List<Line>();
        }
    }

    public partial class BandRate
    {

        private System.Nullable<sbyte> nbLinesField;

        [System.Xml.Serialization.XmlArrayItemAttribute("column", IsNullable = false)]
        public List<Header> header { get; set; }
        [System.Xml.Serialization.XmlArrayItemAttribute("column", typeof(Line), IsNullable = false)]
        public List<Line> lines { get; set; }
        public string type { get; set; }

        public BandRate()
        {
            this.lines = new List<Line>();
            this.header = new List<Header>();
        }

        public sbyte nbLines
        {
            get
            {
                if (this.nbLinesField.HasValue)
                {
                    return this.nbLinesField.Value;
                }
                else
                {
                    return default(sbyte);
                }
            }
            set
            {
                this.nbLinesField = value;
            }
        }

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool nbLinesSpecified
        {
            get
            {
                return this.nbLinesField.HasValue;
            }
            set
            {
                if (value == false)
                {
                    this.nbLinesField = null;
                }
            }
        }
    }
}

Pascal.