Nigh7Sh4de
Nigh7Sh4de

Reputation: 455

XML Serializer Does Not Output All Child Elements

I have an excel file. I am taking the first two columns of the file and using the values of those cells to fill the the first two string members of a class. The rest of the string members of this class are initiated and set in the class constructor. This class is called (ColumnsColumn). I have a second class (called Columns) that has a member that is an array of ColumnsColumn's. This class I am passing as the "object o" parameter in the method XMLSerializer.Selerialize(Stream s, Object o). This method prints an XML to the command line. The problem, is that it is only printing the first two members of ColumnsColumn (the two I got out of the Excel file).

Here is my Program.cs (main) file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Xml.Serialization;

namespace Excel_Dictionary
{
    public class Program
    {
        public static Columns c = new Columns();
        public static List<ColumnsColumn> objects = new List<ColumnsColumn>();

        public static void Main(string[] args)
        {
#if (DEBUG)
            Parser excel = new Parser("stuff.xlsx");
#else
            Console.Write("File name: ");
            Parser excel = new Parser(Console.ReadLine());
#endif
            XmlSerializer x = new XmlSerializer(typeof(Columns));
            List<ColumnsColumn> temp = new List<ColumnsColumn>();
            temp = excel.Parse();
            c.Items = temp.ToArray();
            x.Serialize(Console.Out, c);
            Console.ReadLine();
            for (int i = 0; i < objects.Count; i++)
            {

            }
        }
    }

    public class Parser
    {
        private Excel.Application xlApp;
        private Excel.Workbook xlBook;
        private Excel.Worksheet xlSheet;

        public Parser(string filename)
        {
            xlApp = new Excel.Application();
            xlBook = xlApp.Workbooks.Open(filename);
            xlSheet = xlBook.Worksheets[1];
            xlApp.Visible = true;
            Start();
        }

        public List<ColumnsColumn> Parse()
        {
            List<ColumnsColumn> arr = new List<ColumnsColumn>();
            Start();
            do
            {

                ColumnsColumn t = new ColumnsColumn();
                t.ColumnType = CellValue();
                    Move(0, 1);
                t.FieldKey = CellValue();
                    Move(1, -1);
                t.EmptyValue = "";
                t.FieldParameters = "";
                t.TypeKey = "";
                t.RelationshipIdentifier = "";
                t.Title = "";
                t.Section = "";
                t.ColumnStyle = "";
                t.ShowAggregation = "None";
                t.AggregationTotalLevel = "All";
                t.WeightedByFieldKey = "";
                t.WeightedByFieldParameters = "";
                t.CalculateProportion = "False";
                t.ProportionTo = "GrandTotal";
                t.TotalColumnStyle = "";
                t.GroupColumnStyle = "";
                t.Show = "True";
                arr.Add(t);

            } while (CellValue() != "" && CellValue() != null);
            return arr;
        }

        public void Move(int row, int col)
        {
            xlApp.ActiveCell.get_Offset(row, col).Select();
        }

        /// <returns>Value of Active Cell</returns>
        public string CellValue()
        {
            return xlApp.ActiveCell.Value2;
        }

        public void Start()
        {
            xlSheet.get_Range("A2").Select();
        }
    }
}

and my XMLSchema file which contains the XML deserialized classes (don't be taken off by it's size, it has A LOT of get set commands):

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.5472
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// 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 Columns {

    private ColumnsColumn[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Column", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public ColumnsColumn[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = 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 ColumnsColumn {

    private string columnTypeField;

    private string fieldKeyField;

    private string emptyValueField;

    private string fieldParametersField;

    private string typeKeyField;

    private string relationshipIdentifierField;

    private string titleField;

    private string sectionField;

    private string columnStyleField;

    private string showAggregationField;

    private string aggregationTotalLevelField;

    private string weightedByFieldKeyField;

    private string weightedByFieldParametersField;

    private string calculateProportionField;

    private string proportionToField;

    private string totalColumnStyleField;

    private string groupColumnStyleField;

    private string showField;

    public ColumnsColumn() {
        this.emptyValueField = "";
        this.fieldParametersField = "";
        this.typeKeyField = "";
        this.relationshipIdentifierField = "";
        this.titleField = "";
        this.sectionField = "";
        this.columnStyleField = "";
        this.showAggregationField = "None";
        this.aggregationTotalLevelField = "All";
        this.weightedByFieldKeyField = "";
        this.weightedByFieldParametersField = "";
        this.calculateProportionField = "False";
        this.proportionToField = "GrandTotal";
        this.totalColumnStyleField = "";
        this.groupColumnStyleField = "";
        this.showField = "True";
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string ColumnType {
        get {
            return this.columnTypeField;
        }
        set {
            this.columnTypeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string FieldKey {
        get {
            return this.fieldKeyField;
        }
        set {
            this.fieldKeyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string EmptyValue {
        get {
            return this.emptyValueField;
        }
        set {
            this.emptyValueField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string FieldParameters {
        get {
            return this.fieldParametersField;
        }
        set {
            this.fieldParametersField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string TypeKey {
        get {
            return this.typeKeyField;
        }
        set {
            this.typeKeyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string RelationshipIdentifier {
        get {
            return this.relationshipIdentifierField;
        }
        set {
            this.relationshipIdentifierField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string Title {
        get {
            return this.titleField;
        }
        set {
            this.titleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string Section {
        get {
            return this.sectionField;
        }
        set {
            this.sectionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string ColumnStyle {
        get {
            return this.columnStyleField;
        }
        set {
            this.columnStyleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("None")]
    public string ShowAggregation {
        get {
            return this.showAggregationField;
        }
        set {
            this.showAggregationField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("All")]
    public string AggregationTotalLevel {
        get {
            return this.aggregationTotalLevelField;
        }
        set {
            this.aggregationTotalLevelField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string WeightedByFieldKey {
        get {
            return this.weightedByFieldKeyField;
        }
        set {
            this.weightedByFieldKeyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string WeightedByFieldParameters {
        get {
            return this.weightedByFieldParametersField;
        }
        set {
            this.weightedByFieldParametersField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("False")]
    public string CalculateProportion {
        get {
            return this.calculateProportionField;
        }
        set {
            this.calculateProportionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("GrandTotal")]
    public string ProportionTo {
        get {
            return this.proportionToField;
        }
        set {
            this.proportionToField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string TotalColumnStyle {
        get {
            return this.totalColumnStyleField;
        }
        set {
            this.totalColumnStyleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string GroupColumnStyle {
        get {
            return this.groupColumnStyleField;
        }
        set {
            this.groupColumnStyleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.ComponentModel.DefaultValueAttribute("True")]
    public string Show {
        get {
            return this.showField;
        }
        set {
            this.showField = value;
        }
    }
}

enter image description here

Upvotes: 0

Views: 749

Answers (1)

welegan
welegan

Reputation: 3043

In your schema definition, try removing all of the

[System.ComponentModel.DefaultValueAttribute("")]

lines. The reason (I believe) why the nodes are not printing is because you are setting the other columns to default values, and so there's no reason for them to get serialized from .NET xmlserializer's "point of view". You know, if a class had a point of view.

Upvotes: 2

Related Questions