将Datatable转换为XML字符串时的DateTime问题 [英] DateTime issue when converting Datatable to XML string

查看:121
本文介绍了将Datatable转换为XML字符串时的DateTime问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有DateTime的Datatable列.当我将datatable转换为XML字符串时,我得到了一些带Datetime的后缀值(EG:Date在Datatable中为01/01/1999,但在XML字符串中为''1999- 01-01T00:00:00 + 05:30'')
如何避免这种情况.有什么想法吗?

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?

推荐答案

如此处所讨论的 ^ ]问题中提到的Date 的XML字符串是用于从DataTable写入XML 的格式.在上面的参考中,建议使用string 代替Date的替代方法,以及使用XSLT transformation 的替代方法,如果适合您的要求,可以尝试使用.

我想提供另一种替代方法,将XML 字符串中生成的日期值替换为所需的格式.
假设xmlAsWritten 是使用DataTableWriteXml方法生成的xml文本.然后,将Regex.Replace 方法与如下所示的search and replace模式一起使用,可以将Date 值转换为所需的格式.
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}/


这篇关于将Datatable转换为XML字符串时的DateTime问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆