Tasks can't be added to the project file

Hi Team,


I’m trying to add a new task into the project and then save the project as xml file. But when i open the project file using MS Project, then i can’t able to see the tasks that i have added. I have the code that i’m using to add the task. Let me know whether i’m doing wrong?

//create a prject instance
Project prj = new Project();
//create a project writer instance
ProjectWriter prjWriter = new ProjectWriter();

Task task1 = new Task(“Main task”);
task1.Start = DateTime.Parse(“10-Aug-2011 9:00 AM”);
task1.Finish = DateTime.Parse(“15-Aug-2011 2:00 PM”);

task1.Children.Add(new Task(“sub task1”));
task1.Children.Add(new Task(“sub task2”));

prj.RootTask.Children.Add(task1);

//create a file stream
FileStream st = new FileStream(@“Tasks.xml”, FileMode.Create, FileAccess.Write);

//write the stream into XML format
prjWriter.Write(prj, st, TasksDataFormat.XML);
st.Close();

-Vijay

Hi Vijay,

Please try the following code to add new tasks and feel free to contact us in case you have further comments or questions.

Project project = new Project();
project.MinutesPerDay = 60 * 8;
//Add root task
Task root = new Task();
project.RootTask = root;
//Add child task
Task task = new Task("Task1");
task.ActualStart = DateTime.Parse("10-Aug-2011 9:00 AM");
//set duration in hours
task.Duration = new TimeSpan(24, 0, 0);
task.DurationFormat = TimeUnitType.Day;
root.Children.Add(task);
//We need to recalculate the IDs only as UIDs were set correctly.
project.CalcTaskIds();
project.CalcTaskUids();
ProjectWriter writer = new ProjectWriter();
writer.Write(project, "project.xml", TasksDataFormat.XML);

Best Regards,