简体   繁体   English

将 Timezoneinfo 对象从字符串反序列化为 C# 对象

[英]Deserialize Timezoneinfo object from string into C# object

I am getting timezoneInfo in form of xml string , is there any way to deserialize it into TimzoneInfo c# object?我以 xml 字符串的形式获取 timezoneInfo,有什么方法可以将其反序列化为 TimzoneInfo c# 对象?

Xml String: xml字符串:

<Id xmlns=\"\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d:string\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">Pacific Standard Time (Mexico)</Id><DisplayName xmlns=\"\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d:string\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">(UTC-08:00) Baja California</DisplayName><StandardName xmlns=\"\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d:string\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">Pacific Standard Time (Mexico)</StandardName><DaylightName xmlns=\"\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d:string\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">Pacific Daylight Time (Mexico)</DaylightName><BaseUtcOffset xmlns=\"\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\" i:type=\"d:duration\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">-PT8H</BaseUtcOffset><AdjustmentRules xmlns=\"\" xmlns:d=\"mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561
934e089\" i:type=\"d:System.TimeZoneInfo+AdjustmentRule[]\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><c:TimeZoneInfo.AdjustmentRule xmlns:c=\"http://schemas.datacontract.org/2004/07/System\"><DateStart xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:dateTime\">0001-01-01T00:00:00</DateStart><DateEnd xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:dateTime\">9999-12-31T00:00:00</DateEnd><DaylightDelta xmlns:e=\"http://schemas.microsoft.com/2003/10/Serialization/\" i:type=\"e:duration\">PT1H</DaylightDelta><DaylightTransitionStart i:type=\"d:System.TimeZoneInfo+TransitionTime\"><TimeOfDay xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:dateTime\">0001-01-01T02:00:00</TimeOfDay><Month xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">4</Month><Week xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">1</Week><Day xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">1</Day><DayOfWeek i:type=\"d:System.DayOfWeek\">Sunday</
DayOfWeek><IsFixedDateRule xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:boolean\">false</IsFixedDateRule></DaylightTransitionStart><DaylightTransitionEnd i:type=\"d:System.TimeZoneInfo+TransitionTime\"><TimeOfDay xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:dateTime\">0001-01-01T02:00:00</TimeOfDay><Month xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">10</Month><Week xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">5</Week><Day xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">1</Day><DayOfWeek i:type=\"d:System.DayOfWeek\">Sunday</DayOfWeek><IsFixedDateRule xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:boolean\">false</IsFixedDateRule></DaylightTransitionEnd></c:TimeZoneInfo.AdjustmentRule></AdjustmentRules><SupportsDaylightSavingTime xmlns=\"\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d:boolean\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">true</SupportsDaylightSavingTime>"
<Id xmlns=\"\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d:string\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">Pacific Standard Time (Mexico)</Id><DisplayName xmlns=\"\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d:string\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">(UTC-08:00) Baja California</DisplayName><StandardName xmlns=\"\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d:string\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">Pacific Standard Time (Mexico)</StandardName><DaylightName xmlns=\"\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d:string\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">Pacific Daylight Time (Mexico)</DaylightName><BaseUtcOffset xmlns=\"\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\" i:type=\"d:duration\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">-PT8H</BaseUtcOffset><AdjustmentRules xmlns=\"\" xmlns:d=\"mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561
934e089\" i:type=\"d:System.TimeZoneInfo+AdjustmentRule[]\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><c:TimeZoneInfo.AdjustmentRule xmlns:c=\"http://schemas.datacontract.org/2004/07/System\"><DateStart xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:dateTime\">0001-01-01T00:00:00</DateStart><DateEnd xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:dateTime\">9999-12-31T00:00:00</DateEnd><DaylightDelta xmlns:e=\"http://schemas.microsoft.com/2003/10/Serialization/\" i:type=\"e:duration\">PT1H</DaylightDelta><DaylightTransitionStart i:type=\"d:System.TimeZoneInfo+TransitionTime\"><TimeOfDay xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:dateTime\">0001-01-01T02:00:00</TimeOfDay><Month xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">4</Month><Week xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">1</Week><Day xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">1</Day><DayOfWeek i:type=\"d:System.DayOfWeek\">Sunday</
DayOfWeek><IsFixedDateRule xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:boolean\">false</IsFixedDateRule></DaylightTransitionStart><DaylightTransitionEnd i:type=\"d:System.TimeZoneInfo+TransitionTime\"><TimeOfDay xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:dateTime\">0001-01-01T02:00:00</TimeOfDay><Month xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">10</Month><Week xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">5</Week><Day xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:unsignedByte\">1</Day><DayOfWeek i:type=\"d:System.DayOfWeek\">Sunday</DayOfWeek><IsFixedDateRule xmlns:e=\"http://www.w3.org/2001/XMLSchema\" i:type=\"e:boolean\">false</IsFixedDateRule></DaylightTransitionEnd></c:TimeZoneInfo.AdjustmentRule></AdjustmentRules><SupportsDaylightSavingTime xmlns=\"\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d:boolean\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">true</SupportsDaylightSavingTime>"

I am trying to deserialize using below code我正在尝试使用以下代码反序列化

var timeZoneObj=  TimeZoneInfo.FromSerializedString(timeZoneInfo.Value);

I am getting the exception as An error occurred while deserializing the object.我收到异常,因为反序列化对象时发生错误。 The serialized data is corrupt.序列化数据已损坏。

The exception is justified, because the input string is indeed corrupt, missing its delimiting semicolons.异常是合理的,因为输入字符串确实已损坏,缺少其分隔分号。 Use a valid string generated from TimeZoneInfo.ToSerializedString() as input to TimeZoneInfo.FromSerializedString() .使用从产生有效的字符串TimeZoneInfo.ToSerializedString()输入到TimeZoneInfo.FromSerializedString()

While MSDN ( https://msdn.microsoft.com/en-us/library/system.timezoneinfo.toserializedstring(v=vs.110).aspx ) does not point out any such potential problems, it might be that your input string has been generated on a different platform/encoding/framework version.虽然 MSDN ( https://msdn.microsoft.com/en-us/library/system.timezoneinfo.toserializedstring(v=vs.110).aspx ) 没有指出任何此类潜在问题,但可能是您的输入字符串已在不同的平台/编码/框架版本上生成。

Same problem. 同样的问题。 I get the timezones from IEnumerable allTimeZones = TimeZoneInfo.GetSystemTimeZones(); 我从IEnumerable allTimeZones = TimeZoneInfo.GetSystemTimeZones();获取时区。

taking eg the DisplayName of an element: (UTC+02:00) Harare, Pretoria 以一个元素的DisplayName作为:(UTC + 02:00)哈拉雷,比勒陀利亚

I run: TimeZoneInfo.FromSerializedString( "(UTC+02:00) Harare, Pretoria" ) 我运行:TimeZoneInfo.FromSerializedString(“(UTC + 02:00)哈拉雷,比勒陀利亚”)

in the immediate window which gives an error. 在立即窗口中给出错误。

I also tried the StandardName: South Africa Standard Time but TimeZoneInfo.FromSerializedString( "South Africa Standard Time" ) 我还尝试了StandardName:南非标准时间,但是TimeZoneInfo.FromSerializedString(“南非标准时间”)

also generates the exception. 也生成异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM