469,329 Members | 1,381 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 7232
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Robert Scarborough | last post: by
8 posts views Thread by Abhijit | last post: by
9 posts views Thread by Alok yadav | last post: by
1 post views Thread by Gurunath | last post: by
6 posts views Thread by Steve Ryan | last post: by
2 posts views Thread by Stevienashaa | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.