468,307 Members | 1,326 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,307 developers. It's quick & easy.

HOW to representing NULL for dateTime?

Hi,
I am having the schema and one element in that schema is ,
<xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1"/>

How do I represent the NULL value for the datatype dateTime in the xml.
Nov 12 '05 #1
2 22872
"Pet Matrix." <Pe*******@discussions.microsoft.com> wrote in message news:B8**********************************@microsof t.com...
I am having the schema and one element in that schema is ,
<xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1"/>

How do I represent the NULL value for the datatype dateTime in the xml.


The common practice is probably to change the minOccurs="1" to minOccurs="0",
making the element, EndT, optional.

<xs:element name="EndT" type="TstampType" minOccurs="0" maxOccurs="1"/>

Here the answer to your question is that EndT is NULL when EndT is not present in
the instance document. If you're going to use this schema with an ADO.NET DataSet,
it will be significantly easier going on you when following this approach.

An alternative answer (albeit recognized by the specification as an out-of-band signal
to the XML consumer) is to add the nillable attribute to this element's declaration,

<xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1" nillable="true" />

Then within your instance document whenever the element EndT appears and it
must indicate it's value is 'null' it must appear as follows,

<EndT xsi:nil="true" />

assuming empty content is consistent with your definition of TstampType. You must
also declare the XML Schema Instance document [xsi] namespace URI somewhere
within the scope that includes EndT, i.e.,

<myDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<!-- . . . -->
<EndT xsi:nil="true" />
<!-- . . . -->
</myDocument>

For more information of xsi:nil and using nillable in your schema documents, please
see,

http://www.w3.org/TR/xmlschema-0/#Nils

The final quandry you'll find yourself in with .NET is how to represent a 'null' value
in a .NET DateTime, because the DateTime structure is a value-type (it cannot be
null). The two approaches most commonly used here are to:

1. Represent 'null' with some 'special' date such as DateTime.MinValue or
DateTime.MaxValue. Using MaxValue as the 'null' value for a DateTime
representing start/endpoints in time can be beneficial as it fits well into a
number of date range processing situations (e.g., having a defined Start-
Date and no EndDate, signifying an activity that is 'in-progress').

2. Use System.Data.SqlTypes.SqlDateTime which has a field equating to
Null and an IsNull property, befiting the trilevel-logic found in relational
databases.
Derek Harmon
Nov 12 '05 #2
Thanks Derek.

"Derek Harmon" wrote:
"Pet Matrix." <Pe*******@discussions.microsoft.com> wrote in message news:B8**********************************@microsof t.com...
I am having the schema and one element in that schema is ,
<xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1"/>

How do I represent the NULL value for the datatype dateTime in the xml.


The common practice is probably to change the minOccurs="1" to minOccurs="0",
making the element, EndT, optional.

<xs:element name="EndT" type="TstampType" minOccurs="0" maxOccurs="1"/>

Here the answer to your question is that EndT is NULL when EndT is not present in
the instance document. If you're going to use this schema with an ADO.NET DataSet,
it will be significantly easier going on you when following this approach.

An alternative answer (albeit recognized by the specification as an out-of-band signal
to the XML consumer) is to add the nillable attribute to this element's declaration,

<xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1" nillable="true" />

Then within your instance document whenever the element EndT appears and it
must indicate it's value is 'null' it must appear as follows,

<EndT xsi:nil="true" />

assuming empty content is consistent with your definition of TstampType. You must
also declare the XML Schema Instance document [xsi] namespace URI somewhere
within the scope that includes EndT, i.e.,

<myDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<!-- . . . -->
<EndT xsi:nil="true" />
<!-- . . . -->
</myDocument>

For more information of xsi:nil and using nillable in your schema documents, please
see,

http://www.w3.org/TR/xmlschema-0/#Nils

The final quandry you'll find yourself in with .NET is how to represent a 'null' value
in a .NET DateTime, because the DateTime structure is a value-type (it cannot be
null). The two approaches most commonly used here are to:

1. Represent 'null' with some 'special' date such as DateTime.MinValue or
DateTime.MaxValue. Using MaxValue as the 'null' value for a DateTime
representing start/endpoints in time can be beneficial as it fits well into a
number of date range processing situations (e.g., having a defined Start-
Date and no EndDate, signifying an activity that is 'in-progress').

2. Use System.Data.SqlTypes.SqlDateTime which has a field equating to
Null and an IsNull property, befiting the trilevel-logic found in relational
databases.
Derek Harmon

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by cem marifet | last post: by
8 posts views Thread by Manish Jain | last post: by
1 post views Thread by Dean L. Howen | last post: by
2 posts views Thread by Fred Nelson | last post: by
1 post views Thread by Eric Dev | last post: by
4 posts views Thread by jehugaleahsa | last post: by
2 posts views Thread by Ken Foskey | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.