1 Replies - 121 Views - Last Post: 06 February 2012 - 11:19 PM Rate Topic: -----

Topic Sponsor:

#1 negligible  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 56
  • View blog
  • Posts: 272
  • Joined: 02-December 10

Serializing to Textfile - InvalidOperation

Posted 06 February 2012 - 10:57 PM

I get an InvalidOperationException when trying to reflect the "listing" property, as said by inner exception. When it tries to serialize ArmyListing.

All the variables are public. Checked Lists can be serialized as most errors I found where with people using Dictionaries which can't.

Any idea why it appears not to be serializable?
I have substituted some of the other classes and get the same issue with them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;

namespace ThereIsOnlyRules
{
    [Serializable]
    public class ArmyListing
    {

        [XmlElement("army")]
        public List<Army> listing { get; set; }

        public void SerializeToXML(ArmyListing armyListing)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ArmyListing));
                TextWriter textWriter = new StreamWriter(@"C:\Test\40k.xml");
                serializer.Serialize(textWriter, armyListing);
                textWriter.Close();
            }
            catch (Exception ex) { }
        }
    }

    [Serializable]
    public class Army
    {
        //public Army();

        [XmlAttribute]
        [XmlArray("unit-category")]
        public List<UnitCategory> settingconstraints { get; set; }
        [XmlAttribute("name")]
        public string armyName { get; set; }
    }

    [Serializable]
    public class UnitCategory
    {
        //public UnitCategory();

        [XmlArray("unit-type")]
        public List<UnitType> setting { get; set; }
        [XmlAttribute("name")]
        public string unitCategoryName { get; set; }
    }

    [Serializable]
    public class UnitType
    {
        //public UnitType();

        [XmlArray("unit")]
        public List<Unit> setting { get; set; }
        [XmlAttribute("name")]
        public string unitTypeName { get; set; }
    }

    [Serializable]
    public class Unit
    {
        //public Unit();

        [XmlAttribute("name")]
        public string unitName { get; set; }
        [XmlAttribute("composition")]
        public string compsition { get; set; }
        [XmlAttribute("weapon-skill")]
        public string weaponSkill { get; set; }
        [XmlAttribute("ballistic-skill")]
        public string ballisticSkill { get; set; }
        [XmlAttribute("strength")]
        public string strength { get; set; }
        [XmlAttribute("toughness")]
        public string T { get; set; }
        [XmlAttribute("wounds")]
        public string wounds { get; set; }
        [XmlAttribute("initiative")]
        public string initiative { get; set; }
        [XmlAttribute("attacks")]
        public string attacks { get; set; }
        [XmlAttribute("leadership")]
        public string leadership { get; set; }
        [XmlAttribute("saving-throw")]
        public string saveThrow { get; set; }
        [XmlAttribute("armour")]
        public string armour { get; set; }
        [XmlAttribute("weapons")]
        public string weapons { get; set; }
        [XmlAttribute("special-rules")]
        public string specialRules { get; set; }
        [XmlAttribute("dedicated-transport")]
        public string dedicatedTransport { get; set; }
        [XmlAttribute("options")]
        public string options { get; set; }
    }


}


using System;
using System.Windows.Forms;
using System.Collections.Generic;

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

        private void button1_Click(object sender, EventArgs e)
        {
            ArmyListing armyListing = new ArmyListing();
            armyListing.SerializeToXML(armyListing);
        }
    }
}


This post has been edited by negligible: 06 February 2012 - 11:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Serializing to Textfile - InvalidOperation

#2 negligible  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 56
  • View blog
  • Posts: 272
  • Joined: 02-December 10

Re: Serializing to Textfile - InvalidOperation

Posted 06 February 2012 - 11:19 PM

Never mind, I had the Element and Array tags doubled up.

This post has been edited by negligible: 06 February 2012 - 11:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1