1

I've checked the three hits on SO that the error message's given me. None of them seemed to be relevant (reader on element created, document isn't empty, wrong library used).

My class for Mapping looks like so.

[Serializable]
public class Mapping
{
  [XmlElement] public String Key { get; set; }
  [XmlElement] public String Field { get; set; }
  [XmlElement] public String Value { get; set; }
  [XmlElement] public bool Enable { get; set; }
} 

When I try to assign manually using the code below, I get it to run as supposed to.

return characteristics.ToDictionary(
  element => element.Element("Name").Value,
  element => element.Descendants("DataMapping")
    .Select(felement => new Mapping
    {
      Key = felement.Element("Key").Value
    }));

However, when I try to impose deserialization on the very same data set being read, I get the error in subject.

return characteristics.ToDictionary(
  element => element.Element("Name").Value,
  element => element.Descendants("DataMapping")
    .Select(felement 
      => serializer.Deserialize(felement.CreateReader()) as Mapping));

I know (almost) for sure that I did something stupid and forgot something but I just can't see what... And I'm out of ideas to think of more things to try.

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • Is there an inner exception which might tell you whats wrong? – DerApe Dec 09 '14 at 13:27
  • For XmlSerialization an empty Constructor is needed. –  Dec 09 '14 at 13:37
  • @derape There's no exception thrown at all. Not as far I can see. I'm not catching anything, at least. – Konrad Viltersten Dec 09 '14 at 14:10
  • @wonko79 Isn't an empty constructor available by default unless there's an explicit (and non-empty) one provided? As it is in my case? Or are you saying that we need to have a default constructor **explicitly** declared? I've tried *public Mapping() { }* but got the same misbehavior... – Konrad Viltersten Dec 09 '14 at 14:11
  • @KonradViltersten No constructor means you have a default empty constructor, so that isn't the problem. What if you add a `[XmlRoot("DataMapping")]` to your `Mapping` class? Or just rename it to `DataMapping`. – juharr Dec 09 '14 at 14:23
  • @juharr Bingo! Post it as a reply so I can accept it as an answer. Good call! – Konrad Viltersten Dec 09 '14 at 14:29
  • @KonradViltersten: XmlSerializer requires an explictely implemented empty constructor to work. –  Dec 09 '14 at 15:06
  • @wonko79 I tested adding it and it resulted in no difference. Also, I don't think the computer can distinguish between implicit and explicit empty constructor. At any rate, thanks for trying. See the accepted answer for details. :) – Konrad Viltersten Dec 09 '14 at 15:09
  • 1
    @wonko79 No it doesn't. The default empty constructor will work just fine. – juharr Dec 09 '14 at 15:09

1 Answers1

2

The problem is that your element is named DataMapping and your class is named Mapping. You can either rename the class to DataMapping or add the XmlRoot attribute to the class

[Serializable]
public class DataMapping
{
  ...
}

or...

[XmlRoot("DataMapping")]
public class Mapping
{
  ...
}
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
juharr
  • 31,741
  • 4
  • 58
  • 93