简体   繁体   中英

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 String:

<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() .

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.

Same problem. I get the timezones from IEnumerable allTimeZones = TimeZoneInfo.GetSystemTimeZones();

taking eg the DisplayName of an element: (UTC+02:00) Harare, Pretoria

I run: TimeZoneInfo.FromSerializedString( "(UTC+02:00) Harare, Pretoria" )

in the immediate window which gives an error.

I also tried the StandardName: South Africa Standard Time but TimeZoneInfo.FromSerializedString( "South Africa Standard Time" )

also generates the exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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