我以xml字符串的形式获取timezoneInfo,有没有办法将它反序列化为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>"
我正在尝试使用下面的代码反序列化
var timeZoneObj= TimeZoneInfo.FromSerializedString(timeZoneInfo.Value);
我得到的例外是 反序列化对象时发生错误。序列化数据已损坏。
答案 0 :(得分:0)
异常是合理的,因为输入字符串确实已损坏,缺少分隔分号。使用从TimeZoneInfo.ToSerializedString()
生成的有效字符串作为TimeZoneInfo.FromSerializedString()
的输入。
虽然MSDN(https://msdn.microsoft.com/en-us/library/system.timezoneinfo.toserializedstring(v=vs.110).aspx)没有指出任何此类潜在问题,但可能是您的输入字符串是在不同的平台/编码/框架版本上生成的。