Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a Datatable with DateTime column.When I convert datatable to XML string, I am getting some suffix value with Datetime (EG:Date in Datatable is 01/01/1999, but in XML string it is '1999-01-01T00:00:00+05:30')
How to avoid this.. Any idea?
Posted
Comments
VJ Reddy 24-May-12 8:28am    
Thank you, vivektp, for accepting the solution :)

As discussed here http://stackoverflow.com/questions/6932071/custom-datetime-formats-when-using-dataset-writexml-in-net[^] the XML string of Date mentioned in the question is the format used for writing XML from DataTable. At the above reference an alternative of using string instead of Date and another alternative of using XSLT transformation are suggested, which you may try if it suits your requirement.

I want to give another alternative of replacing the date values generated in the XML string with the desired format.
Let us say xmlAsWritten is the xml text generated using WriteXml method of DataTable. Then using the Regex.Replace method with the patterns for search and replace as shown below the Date values can be converted to the desired format.
C#
string xmlAsWritten = @"<?xml version=""1.0"" standalone=""yes""?>
                            <DocumentElement>
                            <accData>
                                <Date>2012-03-24T00:00:00+05:30</Date>
                                <Credit>500</Credit>
                                <Debit>300</Debit>
                            </accData>
                            <accData>
                                <Date>2012-12-05T00:00:00+05:30</Date>
                                <Credit>350</Credit>
                                <Debit>275</Debit>
                            </accData>
                            </DocumentElement>";
string modifiedXml = Regex.Replace(xmlAsWritten,
                        @"<Date>(?<year>\d{4})-(?<month>\d{2})-(?<date>\d{2}).*?</Date>",    
                        @"<Date>${date}/${month}/${year}</Date>", 
                        RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

//modifiedXml
//<?xml version="1.0" standalone="yes"?>
//<DocumentElement>
//  <accData>
//  <Date>24/03/2012</Date>
//  <Credit>500</Credit>
//  <Debit>300</Debit>
//  </accData>
//  <accData>
//  <Date>05/12/2012</Date>
//  <Credit>350</Credit>
//  <Debit>275</Debit>
//  </accData>
//</DocumentElement>
 
Share this answer
 
v3
Comments
Prasad_Kulkarni 16-May-12 8:41am    
Good one VJ!
+5!
VJ Reddy 16-May-12 8:42am    
Thank you, Prasad :)
Maciej Los 16-May-12 9:40am    
Good work! My 5!
VJ Reddy 16-May-12 10:32am    
Thank you, losmac :)
Asif Huddani 29-Jan-15 1:10am    
superb... this is best...
That's the standard format how datatable convert into XML would work for datetime datatype. If you want to have it in other format, then use string for those dates instead of DateTime.

Similar discussion here: Custom DateTime formats when using DataSet.WriteXml in .NET[^]


Though using an XSLT, you can convert it into any format as desired. You need to decide on it if you want to do that extra step/work for it or not.
 
Share this answer
 
Comments
Prasad_Kulkarni 16-May-12 8:43am    
My +5!
Sandeep Mewara 16-May-12 11:23am    
Thanks.
Maciej Los 16-May-12 9:44am    
The same link as in Solution 1 (VJ Reddy) ;(
But good advise to use XSLT ;)
+5
Sandeep Mewara 16-May-12 11:23am    
Thanks. I did not noticed it. I replied when no answer was there, it looks like we both were replying at the same time. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900