473,809 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Elem entName="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(Elem entName="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 9084
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********@ver dantsys.com> wrote in message
news:9d******** *************** ***@posting.goo gle.com...
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(Elem entName="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(Elem entName="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********@ver dantsys.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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
363
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 as System.DateTime can never be null. So public class MyClass { public string a;
1
3647
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. Function blah(byval User as string, Optional byval UserDate as Date = ?????) Thanks (again)
10
6888
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 there to see if the parameter has been used or not. In vb.net the imported code fails with: Value of type 'Integer' cannot be converted to 'Date'
1
1260
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 only possible with "pattern".
1
1504
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 only possible with "pattern".
2
1443
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 base="xs:string"> <xs:pattern
2
11947
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 procedure. We weren't able to declare it with DateTime.MinValue as its default value, as MinValue is a read-only property rather than a constant. To work around, we had to use a "magic date" that we checked for later on. I was never very happy...
0
1405
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 group 'DateUnionGroup' IS defined.
10
4789
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 value for the parameter of an SqlCommand for inserting the xml document in a column of a table where the column is typed to be of the same xml schema. This all sounds simple, but SQL Server REQUIRES the timezone to be specified for date values....
2
4258
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 search how define type for theses format ? Regards Philippe
1
10378
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7653
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.