如何解析XML日期时间字符串忽略任何时区信息? [英] How to parse an XML datetime string IGNORING any time zone info?

查看:138
本文介绍了如何解析XML日期时间字符串忽略任何时区信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用System.Xml.XmlReader更新从XML文件读取的应用程序,并且需要读取一个Xml日期时间的值,并生成一个字符串表示形式作为本地时间,没有时区信息。

前面的代码只是在固定位置拉出属性值字符串,然后用string.Format()重新构造一个字符串。我觉得这有点脆弱(而且非常难看)。

我将其更改为:

 DateTime runDate = reader.ReadElementContentAsDateTime() ; 
string output = runDate.ToString( MM / dd / yyyy HH:mm:ss,CultureInfo.InvariantCulture);



在我的系统(美国PDT)上,这个似乎完美地工作:

xml日期时间字符串:2015-02-24T10:21:42.3414491-08:00

回复:02/24/2015 10:21:42

然而:单元测试在集成构建服务器上运行时失败,因为该服务器位于不同的时区



那么,有没有办法将字符串解析为忽略时区的本地DateTime,最好没有字符串操作。

我知道我可以使用Regex,但似乎没有使用DateTime作为 日期时间的数据的中间形式。

---

这是非常令人沮丧的,因为ReadElementContentAsDateTime解决方案在转换的正常情况下工作生成后的xml,在同一系统上。这只是构建服务器单元测试失败。



我尝试过:



我尝试了ReadElementContentAsString,然后使用XmlConvert.ToDateTime和各种XmlDateTimeSerializationMode值。

没有运气。

似乎没有办法解析忽略时区信息。

解决方案

不最漂亮的代码,但尝试暂时更改线程当前文化并在之后恢复,这样的事情(顺便检查时区,代码使用US-us作为示例)



 CultureInfo currentCultureInfo = Thread.CurrentThread.CurrentCulture; 
尝试
{
Thread.CurrentThread.CurrentCulture = new CultureInfo ( en-US);
DateTime runDate = reader.ReadElementContentAsDateTime();
string output = runDate.ToString( MM / dd / yyyy HH:mm:ss,CultureInfo.InvariantCulture);
} 最后
{
Thread.CurrentThread.CurrentCulture = currentCultureInfo;
}





祝你好运!



ps。在 ToString 方法中使用 CultureInfo.InvariantCulture 仅用于格式化,而不用于转换或任何其他方法。


I'm updating an application that reads from an XML file using System.Xml.XmlReader and needs to read a value that's an Xml date-time and generate a string representation of that as local time, without time zone info.
The previous code just pulled the attribute value string apart at fixed positions and then re-constructed a string with string.Format(). I thought this was a bit fragile (and very ugly).
I changed it to:

DateTime runDate = reader.ReadElementContentAsDateTime();
string output = runDate.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);


On my system (US PDT), this seemed to work perfectly:
The xml date time string: 2015-02-24T10:21:42.3414491-08:00
Gave back: 02/24/2015 10:21:42
However: This fails the unit tests when they are run on the integration build server, because that server is in a different time zone.

So, is there a way to parse a string into a local DateTime ignoring the time zone, preferably without string manipulation.
I know that I could use a Regex but it just seems wrong not to use DateTime as the intermediate form for data which is a date time.
---
This is quite frustrating, because the ReadElementContentAsDateTime solution does work in the normal case of converting the xml immediately after it is generated, on the same system. It's just the build server unit tests that fail.

What I have tried:

I tried ReadElementContentAsString and then using XmlConvert.ToDateTime with the various XmlDateTimeSerializationMode values.
No luck.
There just doesn't seem to be a way to parse ignoring the timezone info.

解决方案

Not the most beautiful code, but try changing the thread current culture temporarily and restoring afterwards, something like this (Check the timezone by the way, the code uses US-us as an example)

CultureInfo currentCultureInfo = Thread.CurrentThread.CurrentCulture;
try 
{
   Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
   DateTime runDate = reader.ReadElementContentAsDateTime();
   string output = runDate.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
} finally 
{
   Thread.CurrentThread.CurrentCulture = currentCultureInfo;
}



Good luck!

ps. the use of CultureInfo.InvariantCulture in the ToString method is only used for formatting, not for converting or anything.


这篇关于如何解析XML日期时间字符串忽略任何时区信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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