0

我有一个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;
        }
    }
}

在此处输入图像描述

4

1 回答 1

2

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

[System.ComponentModel.DefaultValueAttribute("")]

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

于 2013-07-12T21:27:48.347 回答