【发布时间】:2017-04-29 19:19:22
【问题描述】:
class Program
{
static void Main(string[] args)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"C:\Users\Anmol Jaising\Desktop\Project\File.xml");
Object[] args1 = new Object[1];
XmlNodeList nodeActualResultA = xDoc.SelectNodes("/Report/TestScript/Reporter/ReportItems/ReportItem/ActualResult");
String A = nodeActualResultA[2].InnerText; // 00:04.354
TimeSpan span = Change(A);
String spanTime = span.ToString();
Console.WriteLine(A); // 00:04.354
Console.WriteLine(span); // 00:00.000
Console.WriteLine(spanTime); // 00:00.000
Console.ReadKey();
}
public static TimeSpan Change(string span)
{
TimeSpan interval;
TimeSpan.TryParseExact(span, @"mm\.ss\.fff", null, out interval);
return interval;
}
}
}
如果我以简单的字符串格式使用时间,我会得到完美的输出。但是当我将它转换为 TimeSpan 格式时,无论是什么时间,它都是 00:00.000 mm:ss.000
你能帮帮我吗?
【问题讨论】:
-
旁注:TryParseExact 方法有一个返回一个布尔值,指示它是否可以成功解析给定的字符串。这一切都在MSDN documentation 中编写和解释。