【发布时间】:2014-07-23 22:18:41
【问题描述】:
我有一个包含多个属性的 XML 文件,其中包含时间值,我需要总结这些值。
<test>
<value time="0.345"/>
<value time="0.756"/>
<value time="0.455"/>
</test>
但问题是TimeSpan.Parse("0.345") 不解析值并导致异常。使用TimeSpan.Parse("0.345",System.Globalization.CultureInfo.GetCultureInfo("en-us")) 的事件会导致异常。
Message
Die Zeichenfolge wurde nicht als gültiger TimeSpan erkannt.
StackTrace
bei System.Globalization.TimeSpanParse.TimeSpanResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument, String failureArgumentName)
bei System.Globalization.TimeSpanParse.ProcessTerminal_HM(TimeSpanRawInfo& raw, TimeSpanStandardStyles style, TimeSpanResult& result)
bei System.Globalization.TimeSpanParse.ProcessTerminalState(TimeSpanRawInfo& raw, TimeSpanStandardStyles style, TimeSpanResult& result)
bei System.Globalization.TimeSpanParse.TryParseTimeSpan(String input, TimeSpanStandardStyles style, IFormatProvider formatProvider, TimeSpanResult& result)
bei System.TimeSpan.Parse(String s)
那么将这些时间值解析为 TimeSpan 的正确方法是什么,以便我可以总结这些值?
【问题讨论】:
-
你的
CurrentCulture是什么? -
什么是 0.345 ?分钟?秒?
-
你需要指定时间属性的单位是什么? 0.345s、0.345h 还是什么??
-
@faby 是小数/毫秒秒。 0 秒 345 毫秒
标签: c# parsing format timespan