I am getting a System.FormatException when I try to do the following (as an example):
TimeSpan ts = XmlConvert.ToTimeSpan("P72H");
I’ve investigated the ISO8601 Standard and it seems to be correct, but I cannot get it to parse hours without throwing an exception, no matter what I do.
Thanks!
EDIT:
Exception detail:
System.FormatException was unhandled by user code
HResult=-2146233033
Message=The string 'P72H' is not a valid TimeSpan value.
Source=System.Xml
You need to add the Time separator to your string. Try this:
See the duration specification – http://www.w3.org/TR/xmlschema-2/#duration
Edit/Update based on comments
As there was some question as to why the string
P2M2W5Dwould not be considered a validTimeSpansinceWis part of the ISO 8601 standard, I wanted to add this update so that if someone runs across that issue they don’t have to read through the comments to get the answer. The issue, both for the original string in questionP72HandP2M2W5Dis that the string must conform to the W3C XML Schema (see the documentation for XmlConvert.ToTimeSpan). When we look at the W3C XML Schema (link above), it references back to the ISO 8601 standard, and in particular to section 5.5.3.2.1 which gives the reason whyWis not a valid character in the XML Schema: