DateTime Manipulation with XPathNavigator
In my infopath form I had to set the value of a datetime field by C#. Although the implementation of the XPathNavigator object in InfoPath 2007 exposes the SetTypedValue method — which is used to set a node using a value of a specific type — InfoPath does not implement that method. You must use the SetValue method instead, and pass a string value of the correct format for the data type of the node. [1]
SetValue() is the right choice, but DateTime.Now.ToString() fails, because the XML format is not correct. After some researching I found out, that DateTime.Now.ToString(“s”) is the right choise and the string is well formated. the s-paramter formats the string in the following way: 2008-06-15T21:15:07.
Code Sample:
XPathNavigator form = MainDataSource.CreateNavigator(); XPathNavigator xmlPath = form.SelectSingleNode("/my:Felder/my:Erstelldatum", NamespaceManager); DateTime creationTime = DateTime.Parse(xmlPath.InnerXml); DateTime MinDate = new DateTime(1900, 1, 1); if (creationTime == MinDate) { this.DeleteNil(xmlPath); xmlPath.SetValue(DateTime.Now.ToString("s")); }
[1] http://msdn.microsoft.com/en-us/library/bb509311.aspx

