我在将日期的字符串表示形式转换为日期时间对象时遇到了困难。我的字符串是"2013424"
,如果我使用以下代码将字符串转换为日期时间,那么它会失败..
string format = "yyyyMd";
DateTime.TryParseExact("2013424", format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dtAnswerDate);
dtAnswerDate 不包含字符串值的转换表示。
但是,如果我的字符串具有类似的值,"20130424"
那么它可以正常工作。请问有什么可能的建议吗?
该值来自 Microsoft HealthVault,在我的代码中使用了 XPath 表达式。此 XPath 表达式由最终用户提供。XPath 表达式选择一个节点并使用 InnerText 属性返回值。我正在编写以下代码以供参考。
string strItemXML = healthRecordItem.GetItemXml();
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(strItemXML);
XmlNode xpathNode = xDoc.SelectSingleNode(xpath);
When = healthRecordItem.LastUpdated != null ? healthRecordItem.LastUpdated.Timestamp : healthRecordItem.EffectiveDate;
if (xpathNode != null)
{
return xpathNode.InnerText;
}
“strItemXML”包含以下 XML
`<thing>
<thing-id version-stamp="a40f8c41-0329-4b0f-af56-33040a1f3ecf">de631835-c7b6-43dd-9ae9-528c26ab2204</thing-id>
<type-id name="Appointment">4b18aeb6-5f01-444c-8c70-dbf13a2f510b</type-id>
<thing-state>Active</thing-state>
<flags>0</flags>
<eff-date>2013-04-23T14:33:00Z</eff-date>
<created>
<timestamp>2013-02-15T12:48:42.74Z</timestamp>
<app-id name="InstantPHR Personal Health Record">afc8707e-c160-4b7f-9856-69d5c790b1a9</app-id>
<person-id name="Mohammed Owes">f848a184-878d-4cef-999f-dff54d2e5db2</person-id>
<access-avenue>Online</access-avenue>
<audit-action>Created</audit-action>
<master-app-id>8df99894-5f36-489a-a8fd-ffe6b25b8efb</master-app-id>
</created>
<updated>
<timestamp>2013-04-24T07:44:58.38Z</timestamp>
<app-id name="InstantPHR Personal Health Record">afc8707e-c160-4b7f-9856-69d5c790b1a9</app-id>
<person-id name="Mohammed Owes">f848a184-878d-4cef-999f-dff54d2e5db2</person-id>
<access-avenue>Online</access-avenue>
<audit-action>Updated</audit-action>
<master-app-id>8df99894-5f36-489a-a8fd-ffe6b25b8efb</master-app-id>
</updated>
<data-xml>
<appointment>
<when>
<date>
<y>2013</y>
<m>4</m>
<d>24</d>
</date>
<time>
<h>14</h>
<m>33</m>
<s>0</s>
</time>
</when>
<service>
<text>RREEESSSTTTT</text>
</service>
<clinic>
<name>
<full>Mohammed Owes</full>
</name>
</clinic>
</appointment>
<common>
<source>InstantPHR</source>
<extension source="phr.getrealconsulting.com.appointment_v2"/>
</common>
</data-xml>
</thing>`
并且“xpath”的值为“/thing/data-xml/appointment/when/date”。所以我的代码会返回“2013424”