C# XML Deserializer Not Deserializing Date









up vote
0
down vote

favorite












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?










share|improve this question



















  • 1




    Should your head class contain a policy property?
    – JohnLBevan
    Nov 10 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 dotnetfiddle.net/5veH6q
    – dbc
    Nov 10 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 at 16:50











  • OK, but now I can't reproduce the problem, see dotnetfiddle.net/5veH6q. <dateOfBirth>2000-01-01</dateOfBirth> seems to populate successfully.
    – dbc
    Nov 10 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 at 17:07














up vote
0
down vote

favorite












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?










share|improve this question



















  • 1




    Should your head class contain a policy property?
    – JohnLBevan
    Nov 10 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 dotnetfiddle.net/5veH6q
    – dbc
    Nov 10 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 at 16:50











  • OK, but now I can't reproduce the problem, see dotnetfiddle.net/5veH6q. <dateOfBirth>2000-01-01</dateOfBirth> seems to populate successfully.
    – dbc
    Nov 10 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 at 17:07












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question















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?







c# xml deserialization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 16:49

























asked Nov 10 at 16:41









Gareth

3,43142755




3,43142755







  • 1




    Should your head class contain a policy property?
    – JohnLBevan
    Nov 10 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 dotnetfiddle.net/5veH6q
    – dbc
    Nov 10 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 at 16:50











  • OK, but now I can't reproduce the problem, see dotnetfiddle.net/5veH6q. <dateOfBirth>2000-01-01</dateOfBirth> seems to populate successfully.
    – dbc
    Nov 10 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 at 17:07












  • 1




    Should your head class contain a policy property?
    – JohnLBevan
    Nov 10 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 dotnetfiddle.net/5veH6q
    – dbc
    Nov 10 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 at 16:50











  • OK, but now I can't reproduce the problem, see dotnetfiddle.net/5veH6q. <dateOfBirth>2000-01-01</dateOfBirth> seems to populate successfully.
    – dbc
    Nov 10 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 at 17:07







1




1




Should your head class contain a policy property?
– JohnLBevan
Nov 10 at 16:46




Should your head class contain a policy property?
– JohnLBevan
Nov 10 at 16:46




2




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 dotnetfiddle.net/5veH6q
– dbc
Nov 10 at 16:46




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 dotnetfiddle.net/5veH6q
– dbc
Nov 10 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 at 16:50





@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 at 16:50













OK, but now I can't reproduce the problem, see dotnetfiddle.net/5veH6q. <dateOfBirth>2000-01-01</dateOfBirth> seems to populate successfully.
– dbc
Nov 10 at 16:53




OK, but now I can't reproduce the problem, see dotnetfiddle.net/5veH6q. <dateOfBirth>2000-01-01</dateOfBirth> seems to populate successfully.
– dbc
Nov 10 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 at 17:07




@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 at 17:07












2 Answers
2






active

oldest

votes

















up vote
0
down vote













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);









share|improve this answer



























    up vote
    0
    down vote



    accepted










    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;






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241103%2fc-sharp-xml-deserializer-not-deserializing-date%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      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);









      share|improve this answer
























        up vote
        0
        down vote













        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);









        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          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);









          share|improve this answer












          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);










          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 21:06









          jdweng

          16.3k2716




          16.3k2716






















              up vote
              0
              down vote



              accepted










              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;






              share|improve this answer
























                up vote
                0
                down vote



                accepted










                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;






                share|improve this answer






















                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  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;






                  share|improve this answer












                  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;







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 17:01









                  Gareth

                  3,43142755




                  3,43142755



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241103%2fc-sharp-xml-deserializer-not-deserializing-date%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3