XML序列化程序不输出所有子元素

时间:2013-07-12 20:55:31

标签: c# xml serialization xml-serialization

我有一个excel文件。我正在获取文件的前两列,并使用这些单元格的值来填充类的前两个字符串成员。此类的其余字符串成员在类构造函数中启动并设置。此类称为(ColumnsColumn)。 我有一个第二类(称为Columns),其成员是ColumnsColumn的数组。这个类我作为方法XMLSerializer.Selerialize(Stream s,Object o)中的“object o”参数传递。此方法将XML打印到命令行。 问题是,它只打印了ColumnsColumn的前两个成员(我从Excel文件中取出的两个成员)。

这是我的Program.cs(主要)文件:

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

和我的XMLSchema文件,其中包含XML反序列化的类(不要被它的大小取消,它有很多get set命令):

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

1 个答案:

答案 0 :(得分:2)

在架构定义中,尝试删除所有

[System.ComponentModel.DefaultValueAttribute("")]

行。 (我相信)节点不打印的原因是因为您将其他列设置为默认值,因此没有理由从.NET xmlserializer的“观点”中对它们进行序列化。你知道,如果一个班级有一个观点。

相关问题