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.
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);
?>
//<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>