Am trying to convert my string value(the value is millisecond value) from seconds.
Here is my code am trying to convert inside my xml append
xmlElement = doc.CreateNode(XmlNodeType.Element, "duration", null);
//Convert Milliseconds to Seconds
string durationMilli=DurationValue[1].TrimStart();
TimeSpan ts = TimeSpan.FromSeconds(durationMilli);//tried this didn't work
TimeSpan ts = TimeSpan.FromMilliseconds(durationMilli).TotalSeconds;//then tried this didn't work either
xmlElement.InnerText = DurationValue[1].TrimStart();
newChild.AppendChild(xmlElement);
It’s throwing:
“The best Overloaded method match for System.Timespan.FromMilliseconds(double) has invalid arguments”
am not sure i have to convert the string to actual millisecond value then use timespan to convert to seconds ? please guide me.
Thanks in Advance.
Modified the code little bit and this what am using in my code (this is Answer)
string durationMilli = DurationValue[1].TrimStart();
double milliseconds;
// Try to convert string to double
if (double.TryParse(durationMilli, out milliseconds))
{
// milliseconds now contains your value
double ds = Math.Round(TimeSpan.FromMilliseconds(milliseconds).TotalSeconds);
string totalsec = ds.ToString();
xmlElement.InnerText = totalsec;
newChild.AppendChild(xmlElement);
}
else
{
// durationMilli is not valid double - perhaps it contains letters or some special characters, report an error
}
durationMilli is a string. To try to convert it to double use