Working with Projects
Aspose.Tasks for .NET lets you work with Microsoft Project (MPP/XML) files without having Microsoft Project installed or relying on Microsoft Office Automation. It provides a powerful and flexible API that helps developers save time and effort by offering a consistent way to manipulate project data across different formats.
With Aspose.Tasks, you can:
- Create new project files from scratch or based on templates
- Open existing files in a variety of formats (MPP, MPT, MPX, XML, XER, P6 XML)
- Save projects back in multiple formats, including MPP, XML, PDF, HTML, and more
This article explains how to create, read, and save project files using the Project
class.
Creating an Empty Project File
The Project class is the main class in Aspose.Tasks used to set and get properties associated with a project, as well as behavior. The Save method offered by this class makes it possible to render the Project to various output formats such as XML, MPP, PDF, HTML, etc. with a single API call. This method accepts a file stream or file name, and one of the values provided by the SaveFileFormat enumeration type.
The following lines of code create a simple project file in XML format.
Create an Empty Project And Saving as XML File
This example demonstrates how to create a brand new project and save it as an XML file that can be opened in Microsoft Project.
1Project project = new Project();
2project.Save("EmptyProjectSaveXML_out.xml", SaveFileFormat.XML);
The resulting XML project file can be opened in Microsoft Project using the following steps:
- On the File menu, select Open.
- Select the XML format (*.xml) option from the file types and browse to the output XML file.
- On the Project menu, select Project Information
Create an Empty Project and Save to Stream
If you need to work with projects in-memory (e.g., in web applications or cloud environments), you can save directly to a MemoryStream
instead of writing to a file.
1// Create a project instance
2Project newProject = new Project();
3
4// Create a file stream
5using (FileStream stream = new FileStream("EmptyProjectSaveStream_out.xml", FileMode.Create, FileAccess.Write))
6{
7 // Write the stream into XML format
8 newProject.Save(stream, SaveFileFormat.XML);
9}
Create an Empty Project and Save to MPP
This example shows how to generate a native Microsoft Project MPP file programmatically.
1// there is no more need to load MPP template to save it into MPP
2// add tasks, resources, etc.
3Project project = new Project();
4
5// !The project will be saved into MPP by using internal MPP template.
6project.Save("New Project.mpp", SaveFileFormat.MPP);
Read a Project File
Aspose.Tasks for .NET lets you read existing project in different formats: XML, MPP, MPT, MPX, XER, Primavera P6 XML, etc and save these back in MPP or another format after updating. The following snippets show how a project file can be read using the Project class’s constructor.
Reading Project Files as a Template
This example shows how to load a project using the Project
constructor and treat it as a template for further work.
1// Read existing project template file
2Project project = new Project("New Project.mpp");
Read Project File from Stream
For scenarios where projects are uploaded or stored in streams, you can load them directly from Stream
objects.
1// Read the project xml into file stream
2using (FileStream stream = new FileStream("ReadProjectFileFromStream.xml", FileMode.Open))
3{
4 // Create project using file stream
5 Project project = new Project(stream);
6}
Import Project Data from Microsoft Project Server Database
We plan to retire the importing of project data from Microsoft Project Server Database in a future release. Instead you can use import \ export of project data using Microsoft Project Server’s PWA API.
See documentation for ProjectServerManager class for more details.
1 // Create connection string
2 SqlConnectionStringBuilder sqlConnectionString = new SqlConnectionStringBuilder();
3 sqlConnectionString.DataSource = "192.168.56.2,1433";
4 sqlConnectionString.Encrypt = true;
5 sqlConnectionString.TrustServerCertificate = true;
6 sqlConnectionString.InitialCatalog = "ProjectServer_Published";
7 sqlConnectionString.NetworkLibrary = "DBMSSOCN";
8 sqlConnectionString.UserID = "sa";
9 sqlConnectionString.Password = "*****";
10
11 // Use Aspose.Tasks.Connectivity namespace
12 MspDbSettings settings = new MspDbSettings(sqlConnectionString.ConnectionString, new Guid("E6426C44-D6CB-4B9C-AF16-48910ACE0F54"));
13 Project project = new Project(settings);
Import Project Data from MPD (Microsoft Project Database) File
Older project databases stored in .mpd
format can also be read.
1 DbSettings settings = new MpdSettings("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + "MpdFileToRead.mpd", 1);
2 Project project = new Project(settings);
Ignoring invalid characters during loading Project
Some files may have invalid characters in the custom fields. Microsoft Project does not allow invalid character so the files have been created or manipulated with automation or some other tools. If these be loaded using the API, they may lead to an exception. In order to ignore such invalid characters, the overloaded constructor of Project class can be used with the delegate method ParseErrorCallBack.
1static void Run()
2{
3 // Open modified xml stream
4 using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(GetModifiedXml())))
5 {
6 Project project = new Project(stream, new ParseErrorCallback(CustomDurationHandler));
7 }
8}
9
10static string GetModifiedXml()
11{
12 // Open valid xml file and modify it
13 using (TextReader reader = new StreamReader("IgnoreInvalidCharacters.xml"))
14 {
15 string xml = reader.ReadToEnd();
16 Regex regex = new Regex("PT(\\d+)H(\\d+)M(\\d+)S");
17 return regex.Replace(xml, "**$1Hrs$2Mins$3Secs**");
18 }
19}
20
21static object CustomDurationHandler(object sender, ParseErrorArgs args)
22{
23 Regex regex = new Regex("[*]{2}(\\d+)Hrs(\\d+)Mins(\\d+)Secs[*]{2}");
24 if (args.FieldType == typeof(TimeSpan))
25 {
26 Debug.Print("Object field : {0}, Invalid value : {1}", args.FieldName, args.InvalidValue);
27 string duration = regex.Replace(args.InvalidValue, "PT$1H$2M$3S");
28 TimeSpan newValue = Duration.ParseTimeSpan(duration);
29 Debug.Print("New value : {0}", newValue);
30 return newValue;
31 }
32 // Here we handle only TimeSpan instances, so rethrow original exception with other types
33 throw args.Exception;
34}
Working With Encodings
When working with MPX files, you may need to specify the correct file encoding. This ensures that text data is read and displayed properly. Aspose.Tasks for .NET provides support for the encoding of MPX files. The following code example shows the encoding settings.
1// Specify Encodings
2using (StreamReader streamReader = new StreamReader("Project.mpx", System.Text.Encoding.GetEncoding("ISO-8859-1")))
3{
4 Project project = new Project(streamReader.BaseStream);
5}
Aspose.Tasks for .NET gives developers full control over Microsoft Project files without requiring MS Project itself.
With the Project
class, you can:
- Create new projects (XML, MPP) and save them to file or stream
- Read existing projects in multiple formats
- Work with Project Server and database integrations (legacy support)
- Handle encoding issues and invalid characters gracefully
Whether you are building a desktop solution or a web application, Aspose.Tasks provides a consistent and reliable API to manage project data programmatically.