473,396 Members | 1,703 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,396 software developers and data experts.

Date Formate when using ds.WriteXml

I have a Table in a Typed Dataset which contains a Date field called
EventDate.
I've ensured that the field is defined as Date as opposed to DateTime in the
Typed Dataset.

When I generate an xml file from an instance of this typed dataset using the
ds.WriteXml method, all the dates come out formatted as follows:

<EventDate>2004-01-26T00:00:00.0000000-05:00</EventDate>

which is the DateTime datatype of the W3C XML Schema Part 2: Datatypes

I want the dates to come out as

<EventDate>2004-01-26</EventDate>

which is the Date datatype of the W3C XML Schema Part 2: Datatypes

How to I force the WriteXml method to output dates as the Date datatype as
opposed to the DateTime datatype?
Nov 12 '05 #1
4 7589
"Robert Scarborough" <r0******************@hotmail.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
I have a Table in a Typed Dataset which contains a Date field called
EventDate. I've ensured that the field is defined as Date as opposed
to DateTime in the Typed Dataset. : : <EventDate>2004-01-26T00:00:00.0000000-05:00</EventDate>

which is the DateTime datatype of the W3C XML Schema Part 2: Datatypes

I want the dates to come out as

<EventDate>2004-01-26</EventDate>


I think you'll need to use an XmlTextWriter subclass to intercept the output
of WriteXml( ) when it's writing these dates. Here's a XmlTextWriter to get
you started,

- - - DsDateFilterXmlTextWriter.cs
// . . .
public class DsDateFilterXmlTextWriter : XmlTextWriter
{
private string watchElement;
private bool onWatch;

public DsDateFilterXmlTextWriter( TextWriter writer, string watchElement) : base( writer)
{
this.watchElement = watchElement;
}

public override void WriteStartElement( string prefix, string localName, string ns)
{
base.WriteStartElement( prefix, localName, ns);
if ( 0 == string.Compare( this.watchElement, localName))
{
onWatch = true;
}
}

public override void WriteString( string text)
{
if ( onWatch )
{
try
{
DateTime dt = DateTime.Parse( text);
text = dt.ToString( "yyyy-MM-dd");
}
catch ( FormatException )
{
;
}
onWatch = false;
}
base.WriteString( text);
}
}
// . . .
dataSet1.WriteXml( new DsDateFilterXmlTextWriter( Console.Out, "EventDate") );
// . . .
- - -

The DataSet represents the value with the time zone to ensure it's unambiguous
what date it represents (for instance, at 11 p.m. in Chicago, the date you've
given is January 25, but in London, England, it's January 26).
Derek Harmon
Nov 12 '05 #2
Thanks for the very robust reply.

I understand that the DateTime format contains the time zone information.
But there are many situations where the time zone information isn't
appropriate and just the simple Date is necessary. The W3C recognizes this
by the fact that they also define a simple Date-only format (Namely "Date")
as an alternative to DateTime.

I wish Microsoft would have given us some kind of property to set to control
whether dates are spit out as DateTime or Date rather than forcing DateTime.
In our particular application, we need to send to customers around the
world, lists of dates indicating when bank holidays occur around the world.
It doesn't make sense to have my particular time zone information (Eastern
Standard) included in a list of bank holidays for Tokyo.

The problem with your suggestion for my particular case is that I also want
to be able to use the ds.WriteXmlSchema method to send my clients a schema
which matches my data. So I would also have to do some fiddling with that
to match the changes you've suggested. I suppose I could just create a new
string field in my Datatable to contain the string value of the date, but
then the schema would define that field as a String rather that a Date.
Perhaps your suggestion is my only option and I'll have to also have code to
modify the schema and code to make sure everything is in synch - yecht.

I hope I don't sound like I'm complaining to you. I just hope someone from
Microsoft is watching.

Thanks again for your help.
Bob Scarborough
"Derek Harmon" <lo*******@msn.com> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl...
"Robert Scarborough" <r0******************@hotmail.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
I have a Table in a Typed Dataset which contains a Date field called
EventDate. I've ensured that the field is defined as Date as opposed
to DateTime in the Typed Dataset.

: :
<EventDate>2004-01-26T00:00:00.0000000-05:00</EventDate>

which is the DateTime datatype of the W3C XML Schema Part 2: Datatypes

I want the dates to come out as

<EventDate>2004-01-26</EventDate>


I think you'll need to use an XmlTextWriter subclass to intercept the

output of WriteXml( ) when it's writing these dates. Here's a XmlTextWriter to get you started,

- - - DsDateFilterXmlTextWriter.cs
// . . .
public class DsDateFilterXmlTextWriter : XmlTextWriter
{
private string watchElement;
private bool onWatch;

public DsDateFilterXmlTextWriter( TextWriter writer, string watchElement) : base( writer) {
this.watchElement = watchElement;
}

public override void WriteStartElement( string prefix, string localName, string ns) {
base.WriteStartElement( prefix, localName, ns);
if ( 0 == string.Compare( this.watchElement, localName))
{
onWatch = true;
}
}

public override void WriteString( string text)
{
if ( onWatch )
{
try
{
DateTime dt = DateTime.Parse( text);
text = dt.ToString( "yyyy-MM-dd");
}
catch ( FormatException )
{
;
}
onWatch = false;
}
base.WriteString( text);
}
}
// . . .
dataSet1.WriteXml( new DsDateFilterXmlTextWriter( Console.Out, "EventDate") ); // . . .
- - -

The DataSet represents the value with the time zone to ensure it's unambiguous what date it represents (for instance, at 11 p.m. in Chicago, the date you've given is January 25, but in London, England, it's January 26).
Derek Harmon

Nov 12 '05 #3
For relevant discussion, see
http://blogs.msdn.com/brada/archive/...13/112784.aspx

"Robert Scarborough" <r0******************@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
Thanks for the very robust reply.

I understand that the DateTime format contains the time zone information.
But there are many situations where the time zone information isn't
appropriate and just the simple Date is necessary. The W3C recognizes
this
by the fact that they also define a simple Date-only format (Namely
"Date")
as an alternative to DateTime.

I wish Microsoft would have given us some kind of property to set to
control
whether dates are spit out as DateTime or Date rather than forcing
DateTime.
In our particular application, we need to send to customers around the
world, lists of dates indicating when bank holidays occur around the
world.
It doesn't make sense to have my particular time zone information (Eastern
Standard) included in a list of bank holidays for Tokyo.

The problem with your suggestion for my particular case is that I also
want
to be able to use the ds.WriteXmlSchema method to send my clients a
schema
which matches my data. So I would also have to do some fiddling with that
to match the changes you've suggested. I suppose I could just create a
new
string field in my Datatable to contain the string value of the date, but
then the schema would define that field as a String rather that a Date.
Perhaps your suggestion is my only option and I'll have to also have code
to
modify the schema and code to make sure everything is in synch - yecht.

I hope I don't sound like I'm complaining to you. I just hope someone
from
Microsoft is watching.

Thanks again for your help.
Bob Scarborough
"Derek Harmon" <lo*******@msn.com> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl...
"Robert Scarborough" <r0******************@hotmail.com> wrote in message

news:%2****************@tk2msftngp13.phx.gbl...
> I have a Table in a Typed Dataset which contains a Date field called
> EventDate. I've ensured that the field is defined as Date as opposed
> to DateTime in the Typed Dataset.

: :
> <EventDate>2004-01-26T00:00:00.0000000-05:00</EventDate>
>
> which is the DateTime datatype of the W3C XML Schema Part 2: Datatypes
>
> I want the dates to come out as
>
> <EventDate>2004-01-26</EventDate>


I think you'll need to use an XmlTextWriter subclass to intercept the

output
of WriteXml( ) when it's writing these dates. Here's a XmlTextWriter to

get
you started,

- - - DsDateFilterXmlTextWriter.cs
// . . .
public class DsDateFilterXmlTextWriter : XmlTextWriter
{
private string watchElement;
private bool onWatch;

public DsDateFilterXmlTextWriter( TextWriter writer, string

watchElement) : base( writer)
{
this.watchElement = watchElement;
}

public override void WriteStartElement( string prefix, string

localName, string ns)
{
base.WriteStartElement( prefix, localName, ns);
if ( 0 == string.Compare( this.watchElement, localName))
{
onWatch = true;
}
}

public override void WriteString( string text)
{
if ( onWatch )
{
try
{
DateTime dt = DateTime.Parse( text);
text = dt.ToString( "yyyy-MM-dd");
}
catch ( FormatException )
{
;
}
onWatch = false;
}
base.WriteString( text);
}
}
// . . .
dataSet1.WriteXml( new DsDateFilterXmlTextWriter( Console.Out,

"EventDate") );
// . . .
- - -

The DataSet represents the value with the time zone to ensure it's

unambiguous
what date it represents (for instance, at 11 p.m. in Chicago, the date

you've
given is January 25, but in London, England, it's January 26).
Derek Harmon


Nov 12 '05 #4
Thanks for the info. It makes for interesting reading.

On the one hand, its nice to see that I'm not alone in my problems.

On the other hand, its not nice to see that Microsoft doesn't have a
solution.

Bob Scarborough

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:uL**************@tk2msftngp13.phx.gbl...
For relevant discussion, see
http://blogs.msdn.com/brada/archive/...13/112784.aspx

"Robert Scarborough" <r0******************@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
Thanks for the very robust reply.

I understand that the DateTime format contains the time zone information. But there are many situations where the time zone information isn't
appropriate and just the simple Date is necessary. The W3C recognizes
this
by the fact that they also define a simple Date-only format (Namely
"Date")
as an alternative to DateTime.

I wish Microsoft would have given us some kind of property to set to
control
whether dates are spit out as DateTime or Date rather than forcing
DateTime.
In our particular application, we need to send to customers around the
world, lists of dates indicating when bank holidays occur around the
world.
It doesn't make sense to have my particular time zone information (Eastern Standard) included in a list of bank holidays for Tokyo.

The problem with your suggestion for my particular case is that I also
want
to be able to use the ds.WriteXmlSchema method to send my clients a
schema
which matches my data. So I would also have to do some fiddling with that to match the changes you've suggested. I suppose I could just create a
new
string field in my Datatable to contain the string value of the date, but then the schema would define that field as a String rather that a Date.
Perhaps your suggestion is my only option and I'll have to also have code to
modify the schema and code to make sure everything is in synch - yecht.

I hope I don't sound like I'm complaining to you. I just hope someone
from
Microsoft is watching.

Thanks again for your help.
Bob Scarborough
"Derek Harmon" <lo*******@msn.com> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl...
"Robert Scarborough" <r0******************@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> I have a Table in a Typed Dataset which contains a Date field called
> EventDate. I've ensured that the field is defined as Date as opposed
> to DateTime in the Typed Dataset.
: :
> <EventDate>2004-01-26T00:00:00.0000000-05:00</EventDate>
>
> which is the DateTime datatype of the W3C XML Schema Part 2:
Datatypes >
> I want the dates to come out as
>
> <EventDate>2004-01-26</EventDate>

I think you'll need to use an XmlTextWriter subclass to intercept the

output
of WriteXml( ) when it's writing these dates. Here's a XmlTextWriter

to get
you started,

- - - DsDateFilterXmlTextWriter.cs
// . . .
public class DsDateFilterXmlTextWriter : XmlTextWriter
{
private string watchElement;
private bool onWatch;

public DsDateFilterXmlTextWriter( TextWriter writer, string

watchElement) : base( writer)
{
this.watchElement = watchElement;
}

public override void WriteStartElement( string prefix, string

localName, string ns)
{
base.WriteStartElement( prefix, localName, ns);
if ( 0 == string.Compare( this.watchElement, localName))
{
onWatch = true;
}
}

public override void WriteString( string text)
{
if ( onWatch )
{
try
{
DateTime dt = DateTime.Parse( text);
text = dt.ToString( "yyyy-MM-dd");
}
catch ( FormatException )
{
;
}
onWatch = false;
}
base.WriteString( text);
}
}
// . . .
dataSet1.WriteXml( new DsDateFilterXmlTextWriter( Console.Out,

"EventDate") );
// . . .
- - -

The DataSet represents the value with the time zone to ensure it's

unambiguous
what date it represents (for instance, at 11 p.m. in Chicago, the date

you've
given is January 25, but in London, England, it's January 26).
Derek Harmon



Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robert Scarborough | last post by:
I have a Table in a Typed Dataset which contains a Date field called EventDate. I've ensured that the field is defined as Date as opposed to DateTime in the Typed Dataset. When I generate an...
8
by: Abhijit | last post by:
hello everybody, I got a prob. I see a lot of date formats when I do a search in Google. But can any one give me some pointers / an example to extract today's date in C++ ? (formate of date:...
9
by: Alok yadav | last post by:
i am using a webservice in which a method is serach. i use this method which accept a argument of date type in dd/MM/yyyy formate. i have a textbox which accept the date from the user, when i...
3
by: raahat | last post by:
hi, i need to do desperately the way of storing american DATE formate( like 27-02-2007) in mysql table.i searched on the web a lot,but didnt find anything.PLZ i need someone's help.
1
by: Gurunath | last post by:
Hello All, In database i am storing value in "2007-03-22 02:00:00-05" formate And from input i am taking start date in "2007-3-25" formate and end date in "2007-5-20" formate . Now what i...
2
by: remya1000 | last post by:
How to convert UTC formate time to local time.using vb.net. In my application in some point of the execution i need to retrieve a date,but instead of date i get some numbers,for example...
6
by: Steve Ryan | last post by:
can anyone send me to some good examples of edit in place for data frid columns i see dot net let me assign a control in the IDE any good sources of reading on editing columns with controls...
2
by: Stevienashaa | last post by:
Hello I'm using Access 2003, and I have a query (written in SQL) which has two parameters and asks the user for two dates. This has been working fine. Today I modified the query, removing the...
2
by: kkshansid | last post by:
i want to know how to change date formate in visual basic project from control panel???? from mm/dd/yyyy to dd/mm/yyyy in vb calender control
0
by: kkshansid | last post by:
i know about formate function but i want to confirm that if it has anything to do with system date time in controll panel such that dd/mm/yyyy when i formate system it changes to mm/dd/yyyy...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.