You need to add the Time separator to your string. Try this:
TimeSpan ts = XmlConvert.ToTimeSpan("PT72H");
See the duration specification - http://www.w3.org/TR/xmlschema-2/#duration
3.2.6.1 Lexical representation
The lexical representation for duration is the [ISO 8601] extended format PnYn MnDTnH nMnS, where nY represents the number of years, nM the number of months, nD the number of days, 'T' is the date/time separator, nH the number of hours, nM the number of minutes and nS the number of seconds. The number of seconds can include decimal digits to arbitrary precision.
Edit/Update based on comments
As there was some question as to why the string P2M2W5D
would not be considered a valid TimeSpan
since W
is 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 question P72H
and P2M2W5D
is 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 why W
is not a valid character in the XML Schema:
Since weeks have no defined carry-over point (52 or 53), weeks should
not be used in these applications
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…