序列化分支类(带列表)

时间:2016-04-26 11:25:24

标签: c# xml-serialization

这是我想要序列化的xml脚本:

<?xml version="1.0"?>
<Reports xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ListReportTypes>
    <ReportType Name="ABCD.Record.ABC, abcd, Version=1.0.3.4, Culture=neutral, PublicKeyToken=null">
      <ListOfReportColumns>
        <ColumnProperty DBColumn="System" Display="System" IsColumnDisplay="true" IsSelectedByUser="true" SelectedPosition="0" />
        <ColumnProperty DBColumn="Operation" Display="Operation" IsColumnDisplay="true" IsSelectedByUser="true" SelectedPosition="1" />
        <ColumnProperty DBColumn="FromTime" Display="FromTime" IsColumnDisplay="true" IsSelectedByUser="true" SelectedPosition="2" />
        <ColumnProperty DBColumn="ToDate" Display="ToDate" IsColumnDisplay="true" IsSelectedByUser="true" SelectedPosition="3" />
      </ListOfReportColumns>
    </ReportType>
    <ReportType Name="ExcelReport.SystemModeOfOperationReportRecord, MMM, Version=1.0.3.4, Culture=neutral, PublicKeyToken=null">
      <ListOfReportColumns>
        <ColumnProperty DBColumn="SDF Sig. Level Max." Display="SDF Sig. Level Max." IsColumnDisplay="true" IsSelectedByUser="false" SelectedPosition="-1" />
        <ColumnProperty DBColumn="SDF Max. Emitters" Display="SDF Max. Emitters" IsColumnDisplay="true" IsSelectedByUser="false" SelectedPosition="-1" />
        <ColumnProperty DBColumn="DF RFU Control" Display="DF RFU Control" IsColumnDisplay="true" IsSelectedByUser="false" SelectedPosition="-1" />
        <ColumnProperty DBColumn="SDF Sig. Level Min." Display="SDF Sig. Level Min." IsColumnDisplay="true" IsSelectedByUser="false" SelectedPosition="-1" />
        <ColumnProperty DBColumn="LNA" Display="LNA" IsColumnDisplay="true" IsSelectedByUser="false" SelectedPosition="-1" />
      </ListOfReportColumns>
    </ReportType>
    <ReportType Name="ExcelReport.SystemDeploymentReportRecord, MMM, Version=1.0.3.4, Culture=neutral, PublicKeyToken=null">
      <ListOfReportColumns>
        <ColumnProperty DBColumn="Zone" Display="Zone" IsColumnDisplay="true" IsSelectedByUser="false" SelectedPosition="-1" />
        <ColumnProperty DBColumn="Altitude" Display="Altitude" IsColumnDisplay="true" IsSelectedByUser="false" SelectedPosition="-1" />
        <ColumnProperty DBColumn="MGR" Display="MGR" IsColumnDisplay="true" IsSelectedByUser="false" SelectedPosition="-1" />
        <ColumnProperty DBColumn="North" Display="North" IsColumnDisplay="true" IsSelectedByUser="false" SelectedPosition="-1" />
      </ListOfReportColumns>
    </ReportType>
  </ListReportTypes>
</Reports>

这是我的序列化课程:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;


namespace XML_Example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSerialize_Click(object sender, EventArgs e)
        {
            Reports reports = new Reports();
            reports.listOfReportTypes = new ListReportTypes();
            reports.listOfReportTypes.reportsType.Add(new ReportType());
            reports.listOfReportTypes.reportsType.Add(new ReportType());
            reports.listOfReportTypes.reportsType[0].name="ABCD.Record.ABC, abcd, Version=1.0.3.4, Culture=neutral, PublicKeyToken=null";
            reports.listOfReportTypes.reportsType[0].listOfReportColumns = new ListOfReportColumns();
            reports.listOfReportTypes.reportsType[0].listOfReportColumns.columnProperty.Add(new ColumnProperty());
            reports.listOfReportTypes.reportsType[0].listOfReportColumns.columnProperty.Add(new ColumnProperty());
            reports.listOfReportTypes.reportsType[0].listOfReportColumns.columnProperty.Add(new ColumnProperty());
            Serialize(reports);
            Close();

        }
        static public void Serialize(Reports reports)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Reports));
            using (TextWriter writer = new StreamWriter(@"C:\Users\eladh\Desktop\יצירת XML\Elad.xml"))
            {
                serializer.Serialize(writer, reports);
            }
        }
    }
    [XmlRoot ("Reports")]
    public class Reports
    {
        [XmlElement ("ListReportTypes")]
        public ListReportTypes listOfReportTypes { get; set; }
        public Reports() { }

    }
    public class ListReportTypes
    {
        [XmlElement("ReportType")]
        public List<ReportType> reportsType { get; set; }
        public ListReportTypes() 
        {
            reportsType = new List<ReportType>();
        }

    }
    public class ReportType
    {
        [XmlAttribute ("Name")]
        public string name { get; set; }
        [XmlElement ("ListOfReportColumns")]
        public ListOfReportColumns listOfReportColumns { get; set; }
        public ReportType()
        {
            name = string.Empty;
        }
    }
    public class ListOfReportColumns
    {
        [XmlElement("ColumnProperty")]
        public List<ColumnProperty> columnProperty { get; set; }
        public ListOfReportColumns()
        {
            columnProperty = new List<ColumnProperty>();
        }
    }
    public class ColumnProperty
    {
        [XmlArrayAttribute("DBColumn")]
        public string DBColumn { get; set; }
        [XmlArrayAttribute("Display")]
        public string Display { get; set; }
        [XmlArrayAttribute("IsColumnDisplay")]
        public bool IsColumnDisplay { get; set; }
        [XmlArrayAttribute("IsSelectedByUser")]
        public bool IsSelectedByUser { get; set; }
        [XmlArrayAttribute("SelectedPosition")]
        public int SelectedPosition { get; set; }
        public ColumnProperty()
        {
            DBColumn = string.Empty;
            Display = string.Empty;
        }
    }
}

当我运行它时,它会返回错误:System.InvalidOperationException

有什么问题? 类或序列化是否有问题?

1 个答案:

答案 0 :(得分:1)

问题是您在类ColumnProperty中使用XmlArrayAttribute而不是XmlAttribute。如果查看innerException / innerException / innerException(Many Times),可以看到清除错误消息。