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?
c# xml deserialization
|
show 3 more comments
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?
c# xml deserialization
1
Should yourhead
class contain apolicy
property?
– JohnLBevan
Nov 10 at 16:46
2
The problems are that 1) You are missingpublic policy policy get; set;
onhead
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
|
show 3 more comments
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?
c# xml deserialization
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
c# xml deserialization
edited Nov 10 at 16:49
asked Nov 10 at 16:41
Gareth
3,43142755
3,43142755
1
Should yourhead
class contain apolicy
property?
– JohnLBevan
Nov 10 at 16:46
2
The problems are that 1) You are missingpublic policy policy get; set;
onhead
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
|
show 3 more comments
1
Should yourhead
class contain apolicy
property?
– JohnLBevan
Nov 10 at 16:46
2
The problems are that 1) You are missingpublic policy policy get; set;
onhead
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
|
show 3 more comments
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);
add a comment |
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;
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 10 at 21:06
jdweng
16.3k2716
16.3k2716
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 11 at 17:01
Gareth
3,43142755
3,43142755
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Should your
head
class contain apolicy
property?– JohnLBevan
Nov 10 at 16:46
2
The problems are that 1) You are missing
public policy policy get; set;
onhead
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