Here is another way to accomplish this. This is based on a timesheet
form with dynamically created date ranges for input:
The schema uses 2 repeating tables, payroll periods have 4 or 5 week(s)
and each has 7 day(s):
<weeks>
<week>
<days>
<day><other_fields/></day>
</days>
</week>
</weeks>
The following C# managed code builds the date range array, then loops
each day and determines its week and day nodes and creates them if they
don't exist. You can see that I grab an existing node and just
AppendChild to its parent node.
private void BuildTimesheet(DateTime startingDate, DateTime endingDate)
{
// Calculate the date range
DateTime[] dtRange = this.FillDays(startingDate, endingDate);
XPathNavigator root = this.MainDataSource.CreateNavigator();
// Set some initial node elements
XPathNavigator nodeWeeks =
root.SelectSingleNode("/my:TimesheetData/my:weeks", NamespaceManager);
XPathNavigator nodeWeek =
nodeWeeks.SelectSingleNode("my:week", NamespaceManager);
XPathNavigator nodeDay;
foreach (DateTime date in dtRange)
{
string dayOfWeek = ((int)date.DayOfWeek + 1).ToString();
nodeDay = nodeWeek.SelectSingleNode("my:days/my:day[" +
dayOfWeek + "]", NamespaceManager);
if (nodeDay == null)
{
XPathNavigator tmpNodeDays =
nodeWeek.SelectSingleNode("my:days", NamespaceManager);
XPathNavigator tmpNodeDay =
tmpNodeDays.SelectSingleNode("my:day[1]", NamespaceManager);
tmpNodeDays.AppendChild(tmpNodeDay);
nodeDay =
nodeWeek.SelectSingleNode("my:days/my:day[" + dayOfWeek + "]",
NamespaceManager);
}
// Fill in the Date using "Jan 13" format
XPathNavigator myDate =
nodeDay.SelectSingleNode("my:date", NamespaceManager);
this.SetNilValue(myDate, String.Format("{0} {1}",
date.ToString("MMM"), date.ToString("%d")));
// Create a new week node and append it
if (date.DayOfWeek == DayOfWeek.Saturday && date !=
endingDate)
{
XPathNavigator tmpWeekNode =
nodeWeeks.SelectSingleNode("my:week[1]", NamespaceManager);
nodeWeeks.AppendChild(tmpWeekNode);
nodeWeek =
nodeWeeks.SelectSingleNode("my:week[last()]", NamespaceManager);
}
}
}
Bob Silva