0

I'm having trouble with XML deserialization in C#. I have the following XML:

<?xml version="1.0" encoding="utf-8"?>
<head>
  <person>
    <name>Jim Bob</name>
    <dateOfBirth>1990-01-01</dateOfBirth>
  </person>
  <policy>
    <number>1</number>
    <pet>
      <name>Snuffles</name>
      <dateOfBirth>2000-01-01</dateOfBirth>
    </pet>
  </policy>
</head>

With this I'm trying to map it to the following classes:

public class head
{
    public policy policy { get; set; }
    public person person { get; set; }
}

public class person
{
    public string name { get; set; }
    public DateTime dateOfBirth { get; set; }

    [XmlElement("policy")]
    public List<policy> policy { get; set; }
}

public class policy
{
    public string number { get; set; }
    [XmlElement("pet")]
    public List<pet> pet { get; set; }
}

public class pet
{
    public string name { get; set; }
    [XmlElement("dateOfBirth")]
    public DateTime dateOfBirth { get; set; } //<~~ Issue is with this property
}

The issue is that the dateOfBirth property in the pet class isn't being populated when being deserialized and I don't know why. Is this because of a naming conflict with the dateOfBirth property in the person class?

Gareth
  • 5,140
  • 5
  • 42
  • 73
  • 1
    Should your `head` class contain a `policy` property? – JohnLBevan Nov 10 '18 at 16:46
  • 2
    The problems are that 1) You are missing `public policy policy { get; set; }` on `head` and 2) `[XmlElememnt("dateOfBirth")]` is misspelled and does not compile. Fixing those your code works, see https://dotnetfiddle.net/5veH6q – dbc Nov 10 '18 at 16:46
  • @dbc Thanks, I've updated the question as that was a quickly typed example of the issue. I've rectified the typo / missing property as they are present in the full code. – Gareth Nov 10 '18 at 16:50
  • OK, but now I can't reproduce the problem, see https://dotnetfiddle.net/5veH6q. `2000-01-01` seems to populate successfully. – dbc Nov 10 '18 at 16:53
  • @dbc Ok thanks, I've ran my full code through the same fiddle and I'm getting the same error. You've answered my question anyway that it's not a naming conflict. I'll keep picking away at it! Thanks – Gareth Nov 10 '18 at 17:07
  • Datetime is expecting the date and time combination but in your xml it contains only the date part like 2000-01-01 and the time part is missing. So you have to parse the string to datetime manually – Hassaan Nov 10 '18 at 18:19
  • @Hassaan : That doesn't make any sense. The issue is a DateTime cannot be null. – jdweng Nov 10 '18 at 20:59
  • @Gareth, seems to populate correctly for me. can you show us the code you use to deserialize the xml? – koolkoda Nov 10 '18 at 22:00

2 Answers2

0

Try following code which uses ParseExact. If you are still getting an issue you may have to handle cases where the DateTime is null :

    public class pet
    {
        public string name { get; set; }
        private DateTime _dateOfBirth { get; set; } //<~~ Issue is with this property

        [XmlElement("dateOfBirth")]
        public string DateOfBirth
        {
            get { return _dateOfBirth.ToString("yyyy-MM-dd"); }
            set { _dateOfBirth = DateTime.ParseExact(value, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); }
        }


    }
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

I managed to solve this by using the [XmlElementAttribute(DataType = "date")] attribute on the dateOfBirth field . The revised class that works looks like this:

public class pet
{
    public string name { get; set; }
    [XmlElementAttribute(DataType = "date")]
    public DateTime dateOfBirth { get; set; }
}
Gareth
  • 5,140
  • 5
  • 42
  • 73