471,573 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,573 software developers and data experts.

XmlSerializer for optional xs:date

I have several elements that have optional child elements and
attributes that are xs:dates.
I have been using System.DateTime and the XmlAttribute/XmlElement
attributes, but these will never work as System.DateTime can never be
null.

So
public class MyClass
{
[XmlAttribute("a")] public string a;
[XmlElement(ElementName="b", DataType="date")] public DateTime b;
}

will return
<MyClass>
<b>0001-01-01</b>
</MyClass>

when I want an empty element.
I have tried my own class (Date) that exposes an XmlText attribute:

public class Date
{
private string dt;
public Date() { }
[XmlText()]
public string Value { get { return dt;} set { dt=value;}}
}

and changed my class to:

public class MyClass
{
[XmlAttribute("a")] public string a;
[XmlElement(ElementName="b")] public Date b;
}

....but the serializer then throws an exception :

"Cannot serialize member 'b'. XmlAttribute/XmlText cannot be used to
encode complex types."

How do I achieve an optional xs:date using XmlSerializer ?
Nov 12 '05 #1
3 8935
Hey Dominic,
There is a feature in the .NET XML Serialization engine that allows you to
optionally serialize value types, in other words, types that cannot take the
value null. Such types include bool, int, float, and DateTime, to name just
a few.

For the doc on this, see
http://msdn.microsoft.com/library/en...ClassTopic.asp

Here is a prior thread on the issue (May 2004)
http://tinyurl.com/4lge9

And here is an example illustrating the point for you
http://www.winisp.net/cheeso/srcview...ateOptional.cs

-D
"Dominic Messenger" <dm********@verdantsys.com> wrote in message
news:9d**************************@posting.google.c om...
I have several elements that have optional child elements and
attributes that are xs:dates.
I have been using System.DateTime and the XmlAttribute/XmlElement
attributes, but these will never work as System.DateTime can never be
null.

So
public class MyClass
{
[XmlAttribute("a")] public string a;
[XmlElement(ElementName="b", DataType="date")] public DateTime b;
}

will return
<MyClass>
<b>0001-01-01</b>
</MyClass>

when I want an empty element.
I have tried my own class (Date) that exposes an XmlText attribute:

public class Date
{
private string dt;
public Date() { }
[XmlText()]
public string Value { get { return dt;} set { dt=value;}}
}

and changed my class to:

public class MyClass
{
[XmlAttribute("a")] public string a;
[XmlElement(ElementName="b")] public Date b;
}

...but the serializer then throws an exception :

"Cannot serialize member 'b'. XmlAttribute/XmlText cannot be used to
encode complex types."

How do I achieve an optional xs:date using XmlSerializer ?

Nov 12 '05 #2
Thanks. This is just what I needed.
Unfortunately, the XmlSerializer doesn't have a wealth of examples, so
finding out these things takes time.

Dominic

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3
I feel your pain....
And this feature is particularly obscure and poorly documented.

-D
"Dominic Messenger" <dm********@verdantsys.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Thanks. This is just what I needed.
Unfortunately, the XmlSerializer doesn't have a wealth of examples, so
finding out these things takes time.

Dominic

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dominic Messenger | last post: by
1 post views Thread by Learning SQL Server | last post: by
10 posts views Thread by John Austin | last post: by
1 post views Thread by Stefan Reiter | last post: by
1 post views Thread by Stefan Reiter | last post: by
2 posts views Thread by ZagrebMike | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.