364,085 Members | 5253 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

XmlSerializer for optional xs:date

Dominic Messenger
P: n/a
Dominic Messenger
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
Share this Question
Share on Google+
3 Replies


Dino Chiesa [Microsoft]
P: n/a
Dino Chiesa [Microsoft]
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" <dmessenger@verdantsys.com> wrote in message
news:9d6bd82b.0407210845.228af00a@posting.google.c om...[color=blue]
> 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 ?[/color]


Nov 12 '05 #2

Dominic Messenger
P: n/a
Dominic Messenger
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

Dino Chiesa [Microsoft]
P: n/a
Dino Chiesa [Microsoft]
I feel your pain....
And this feature is particularly obscure and poorly documented.

-D


"Dominic Messenger" <dmessenger@verdantsys.com> wrote in message
news:%23vUgPCOcEHA.1656@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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![/color]


Nov 12 '05 #4

Post your reply

Help answer this question



Didn't find the answer to your .NET Framework question?

You can also browse similar questions: .NET Framework