使用 .Net XML 解析器时丢失时区信息
这是我使用 xml-web 界面中的数据时遇到的问题。
该接口为我提供了带有大量数据块的正确数据,如下所示:
<item>
<date>2011-01-19T09:02:00+01:00</date>
<open>46.625</open>
<high>46.625</high>
<low>46.62</low>
<close>46.62</close>
<volume>827</volume>
<count>2</count>
<type>TRADE</type>
</item>
.Net XML-Decoder (System.XML.Serialization.XmlSerializer) 将其(根据我的 xsd 方案)解析为包含“日期”属性的对象。
以下是从 xsd 生成的代码片段:
[System.CodeDom.Compiler.GenerateCodeAttribute("xsd", “2.0.50727.3038”)] [系统.SerializedAttribute()] [系统.诊断.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“代码”)] 公共部分类 TimeseriesElement {
private System.DateTime dateField; /// <备注/>> 公共 System.DateTime 日期 { 得到 { 返回 this.dateField; } 放 { this.dateField = 值; } } }
从这个派生出一个包含其他值的派生类。我想这里没有什么有趣的东西......对我来说问题是有关时区的信息已经消失了。是的,时间戳已正确修改为系统运行所在的当前活动时区。是的,只有当用户位于与 +1 不同的时区时才会发生这种情况。
我不希望这样,至少不总是这样。大多数时候我对此感到满意,但在某些情况下我不想更改时间戳并在其本机(+1)时区中使用它。遗憾的是,我丢失了时间戳发送的时区信息(或者我在解析生效后没有找到提取此信息的方法),因此我无法更改时间戳以使其再次满足我的需求。
有什么想法吗?哦,还有一件事。更改 xml 不是一个选项,所以我必须自己解决这个问题。
编辑:错别字和对评论的答复
here is my problem using data from a xml-webinterface.
The interface provides me with correct data with lots of datablocks like this one:
<item>
<date>2011-01-19T09:02:00+01:00</date>
<open>46.625</open>
<high>46.625</high>
<low>46.62</low>
<close>46.62</close>
<volume>827</volume>
<count>2</count>
<type>TRADE</type>
</item>
The .Net XML-Decoder (System.XML.Serialisation.XmlSerializer) parses this (according to my xsd sceme) into an object containing a "date" attribute.
Here is a code snipped generated from the xsd:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd",
"2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class TimeseriesElement {private System.DateTime dateField; /// <remarks/> public System.DateTime date { get { return this.dateField; } set { this.dateField = value; } } }
There is one derivation from this containing the other values. Nothing interesting to see here, i guess.... The problem for me is that the information, regarding the timezone, is already gone. Yes, the timestamp is correctly modified to the currently active Timezone the system is running in. And yes, this only occures when the user is in a different timezone than +1.
I dont want that, at last not always. Most of the time i am fine with this, but there are cases where i dont want to change the timestamp and use it in its native (+1) timezone. Sadly i loose the information in what timezone the timestamp was delivered (or i did not found a way to extract this information after the parsing took effect), so i cannot alter the timestamp to make it fit my needs again.
Any Ideas? Oh, one more thing. Changing the xml is not an option, so i have to take care of this problem on my side.
edit: typos & answers to the comments
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试更改 .NET 代码以将其解析为 DateTimeOffset 而不是 DateTime:
DateTimeOffset 结构应保留原始时间的偏移量。
谢谢,
标记
Try changing your .NET code to parse it as a DateTimeOffset rather than a DateTime:
The DateTimeOffset structure should preserve the offset of the original time.
thanks,
mark