473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 9028
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dominic Messenger | last post by:
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...
1
by: Learning SQL Server | last post by:
What should the default value for an optional Date parameter be in a function if I want it to default to Nothing? Setting the optional value to "" fails, so does setting it to Nothing. ...
10
by: John Austin | last post by:
I am migrating my first vb6 app to vb.net 2003. In the vb6 app a number of subs had optional date parameters: Sub Fred ( ... ,optional FromDate as Date = 0...) I need something like zero in...
1
by: Stefan Reiter | last post by:
Hi, I want to limit a date to a minDate and a maxDate - is that possible, and how? <xs:simpleType> <xs:restriction base="xs:date"> <xs: </xs:restriction> </xs:simpleType> I assume it is...
1
by: Stefan Reiter | last post by:
Hi, I want to limit a date to a minDate and a maxDate - is that possible, and how? <xs:simpleType> <xs:restriction base="xs:date"> <xs: </xs:restriction> </xs:simpleType> I assume it is...
2
by: ZagrebMike | last post by:
Hi, I want to create a simpleType that will constrain an element to be a date of the form dd/mm/yyyy. I have this at the moment, <xs:simpleType name="newDate"> <xs:restriction...
2
by: Oenone | last post by:
In our applications, we use the special value of DateTime.MinValue to represent "null dates" throughout all our code. We recently ran into an issue where we wanted an optional date parameter for a...
0
by: ombralonga | last post by:
Hi, I'm getting the following validation error with XMLspy when validating an "xsd" file: Schema Error: the group 'DateUnionGroup' is undefined However, in the xsd document the...
10
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the...
2
by: pcouas | last post by:
Hi, I need to create an XSD file for XML document In mys XML document i could have various date format YYYYMMDD, YYMMDD, DDMMYY, DDMMYYYY I know theses formats before creating xsd file, but i...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.