How to Read Project Files from Stream or from Microsoft Database inside .NET App
This technical tip explains, how .NET developers can read Project files in different ways inside their .NET applications. We mentioned in the earlier articles that Aspose.Tasks can export to Microsoft Project's XML format only. This is true if you are creating a new project. Aspose.Tasks lets you read existing MPP files and save these back in MPP format after updating. This allows reading MPP as well as MPT formats as input templates. This article shows how a Project file (XML, MPP, MPT) can be read using the Project class's constructor. This article also provides the capability to read Project data from Primavera Database. Some files may have invalid characters in the custom fields. MS 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 exception. In order to ignore such invalid characters, the overloaded constructor of Project class can be used with the delegate method ParseErrorCallBack.
//your code here...//Reading Project Files as a Template
//C# Code Sample
//Read a project from template
Project project = new Project("d:\\Project1.mpp");
//VB.NET Code Sample
'Read a project from template
Dim project As Project = New Project("d:\\Project1.mpp")
//Reading Project File from Stream
//C# Code Sample
using(Stream fs = new FileStream("pathToFile.xml", FileMode.Open))
{
project = new Project(fs);
}
//VB.NET Code Sample
Using fs As Stream = New FileStream("pathToFile.xml", FileMode.Open)
project = New Project(fs)
End Using
//Importing Project Data From Microsoft Project Database
//C# Code Sample
SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
sb.DataSource = "192.168.56.2,1433";
sb.Encrypt = true;
sb.TrustServerCertificate = true;
sb.InitialCatalog = "ProjectServer_Published";
sb.NetworkLibrary = "DBMSSOCN";
sb.UserID = "sa";
sb.Password = "*****";
//use Aspose.Tasks.Connectivity namespace
MspDbSettings settings = new MspDbSettings(sb.ConnectionString, new Guid("E6426C44-D6CB-4B9C-AF16-48910ACE0F54"));
Project project = new Project(settings);
//VB.NET Code Sample
Dim sb As New SqlConnectionStringBuilder()
sb.DataSource = "192.168.56.2,1433"
sb.Encrypt = True
sb.TrustServerCertificate = True
sb.InitialCatalog = "ProjectServer_Published"
sb.NetworkLibrary = "DBMSSOCN"
sb.UserID = "sa"
sb.Password = "*****"
'use Aspose.Tasks.Connectivity namespace
Dim settings As New MspDbSettings(sb.ConnectionString, New Guid("E6426C44-D6CB-4B9C-AF16-48910ACE0F54"))
Dim project As New Project(settings)
//Ignoring invalid characters during loading Project
//C# Code Sample
// open modified xml stream
using(MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(GetModifiedXml())))
{
Project project = new Project(stream, new ParseErrorCallback(CustomDurationHandler));
}
private string GetModifiedXml()
{
string xml;
// open valid xml file and modify it
using (TextReader reader = new StreamReader("NewProductDev.xml"))
xml = reader.ReadToEnd();
Regex regex = new Regex("PT(\\d+)H(\\d+)M(\\d+)S");
return regex.Replace(xml, "**$1Hrs$2Mins$3Secs**");
}
private object CustomDurationHandler(object sender, ParseErrorArgs args)
{
Regex regex = new Regex("[*]{2}(\\d+)Hrs(\\d+)Mins(\\d+)Secs[*]{2}");
if (args.FieldType == typeof (TimeSpan))
{
Debug.Print("Object field : {0}, Invalid value : {1}", args.FieldName, args.InvalidValue);
string duration = regex.Replace(args.InvalidValue, "PT$1H$2M$3S");
TimeSpan newValue = Duration.ParseTimeSpan(duration);
Debug.Print("New value : {0}", newValue);
return newValue;
}
// here we handle only TimeSpan instances, so rethrow original exception with other types
throw args.Exception;
}
//VB.NET Code Sample
' open modified xml stream
Using stream As New MemoryStream(Encoding.UTF8.GetBytes(GetModifiedXml()))
Dim project As New Project(stream, New ParseErrorCallback(CustomDurationHandler))
End Using
Private Function GetModifiedXml() As String
Dim xml As String
' open valid xml file and modify it
Using reader As TextReader = New StreamReader("NewProductDev.xml")
xml = reader.ReadToEnd()
End Using
Dim regex As New Regex("PT(\d+)H(\d+)M(\d+)S")
Return regex.Replace(xml, "**$1Hrs$2Mins$3Secs**")
End Function
Private Function CustomDurationHandler(sender As Object, args As ParseErrorArgs) As Object
Dim regex As New Regex("[*]{2}(\d+)Hrs(\d+)Mins(\d+)Secs[*]{2}")
If args.FieldType = GetType(TimeSpan) Then
Debug.Print("Object field : {0}, Invalid value : {1}", args.FieldName, args.InvalidValue)
Dim duration__1 As String = regex.Replace(args.InvalidValue, "PT$1H$2M$3S")
Dim newValue As TimeSpan = Duration.ParseTimeSpan(duration__1)
Debug.Print("New value : {0}", newValue)
Return newValue
End If
' here we handle only TimeSpan instances, so rethrow original exception with other types
Throw args.Exception
End Function
Url: http://www.aspose.com/docs/display/tasksnet/Reading+a+Project+File
Language: C# | User: Sheraz Khan | Created: Sep 16, 2015 | Tags: Read Project Files from Stream read Project data from Primavera Database Reading Project Files as a Template Importing Project Data From MS Project Database .NET Project management Ignoring invalid characters during loading Project