0

I have this class called identity

namespace Game.World.Entities
{
    [XmlRoot("Identity")]
    public class Identity
    {
        public string Name { get; set; }
        public string Texture { get; set; }
        public string HeroType { get; set; }

        public Stats Stats { get; set; }
        public ActiveAbilitites ActiveAbilitites { get; set; }
        public PassiveAbilitites PassiveAbilitites { get; set; }
    }

    public class ActiveAbilitites
    {
        [XmlElement("AbilityId")]
        public List<int> ActiveAbilitiesId { get; set; }
    }

    public class PassiveAbilitites
    {
        [XmlElement("AbilityId")]
        public List<int> PassiveAbilitiesId { get; set; }
    }

    public class Stats
    {
        public int Health { get; set; }
        public int MagicDamage { get; set; }
        public int PhysicalDamage { get; set; }
        public int Defense { get; set; }
    }
}

I've serialized that class into a xml document:

<?xml version="1.0" encoding="utf-8"?>
<Identity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>foo</Name>
  <Texture>player</Texture>
  <HeroType>tracker</HeroType>
  <Stats>
    <Health>5</Health>
    <MagicDamage>5</MagicDamage>
    <PhysicalDamage>5</PhysicalDamage>
    <Defense>5</Defense>
  </Stats>
  <ActiveAbilitites>
    <AbilityId>1</AbilityId>
    <AbilityId>2</AbilityId>
    <AbilityId>3</AbilityId>
  </ActiveAbilitites>
  <PassiveAbilitites>
    <AbilityId>1</AbilityId>
    <AbilityId>2</AbilityId>
    <AbilityId>3</AbilityId>
  </PassiveAbilitites>
</Identity>

However when I try and deseralize the xml into an identity object I get errors

There is an error in the xml document (2,2) .....
System.InvalidOperationException was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read6_ArrayOfIdentity()

Now this is stumping me because the xml comes from the seralization so I have no idea why I can't deseralize it. I'm presuming I have missed an annotation tag somewhere any help on the matter would be amazing.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
zidsal
  • 577
  • 1
  • 7
  • 30
  • Can you show us the code you use for bot serializing and deserializing? – Wouter de Kort Sep 15 '12 at 20:58
  • turns out it was the deseralizing code, it works fine if I read it from the .xml file however my error was happening when I was deseralizing the xml from a string. I'll update this comment when I get it working with a string edit no idea what I did to mess up the deseralizer that much but I now have it working with a string. var reader = new StringReader(template); var identity = (Identity) serializer.Deserialize(reader); – zidsal Sep 15 '12 at 21:14
  • Please do not put the answer in the question. I removed it. – Emond Sep 15 '12 at 21:23

1 Answers1

0

This turned out to be a problem with how I was deseralizing the code. I have no idea what I did to mess it up so much but I'm presuming it was to do with my string reader. I just copied and pasted some example code from Deserialize from string instead TextReader and its now working fine.

Community
  • 1
  • 1
zidsal
  • 577
  • 1
  • 7
  • 30