Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Timezoneinfo对象从字符串反序列化为C#对象_C#_Serialization - Fatal编程技术网

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

将Timezoneinfo对象从字符串反序列化为C#对象,c#,serialization,C#,Serialization,我正在以xml字符串的形式获取timezoneInfo,有没有办法将其反序列化为TimzoneInfo c#对象 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><

我正在以xml字符串的形式获取timezoneInfo,有没有办法将其反序列化为TimzoneInfo c#对象

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>"
我得到的例外情况如下
反序列化对象时出错。序列化数据已损坏。

异常是正确的,因为输入字符串确实已损坏,缺少分隔分号。使用从
TimeZoneInfo.ToSerializedString()生成的有效字符串作为
TimeZoneInfo.FromSerializedString()的输入


虽然MSDN()没有指出任何此类潜在问题,但可能是您的输入字符串是在不同的平台/编码/框架版本上生成的。

更新了问题此函数存在()并用于执行您想要的操作,那么问题是什么?调用
FromSerializedString
是否失败?您是否自己序列化了该字符串?您发布的字符串是否与作为参数传递给
FromSerializedString
timeZoneInfo.value
的运行时值相同?字符串似乎有分隔符问题。请确保逐字记录
到SerializedString
将返回的内容。是,timeZoneInfo。作为参数传递给FromSerializedString的值
var timeZoneObj=  TimeZoneInfo.FromSerializedString(timeZoneInfo.Value);