MPXJ in .NET

This article explains how to access a Microsoft Project Plan File using the MPXJ Library and C#.

Introduction

If data resides in Excel then you have many options to extract the data irrespective of technology. But if the same requirement is for a Microsoft Project Plan (MPP) file then you have a limited number of options.

MPXJ is an open source library to retrieve data from a MPP file. This library provides a set of features to allow project information to be manipulated in Java and .Net.

MPXJ supports many data formats, including the following:

  • Microsoft Project (MPP, MPT)
  • Microsoft Project Exchange (MPX)
  • XML
  • MPD

MPXJ is based around a "neutral" data structure for representing the project data, coupled with a set of format-specific reader and writer classes that understand how to read from and write to the various supported file formats. The library is currently based around a set of structures modeled on the data described by the MPP file format. All manipulation of project data takes place using these data structures that can be read from or written to the various supported file formats.

The figure below illustrates the key entities represented by the MPXJ data structure.

MPXJ data structure
MPXJ currently allows project data to be read from eight distinct data sources using the following reader classes:

  • net.sf.mpxj.mpp.MPPReader: reads Microsoft Project MPP files
  • net.sf.mpxj.mpx.MPXReader: reads Microsoft MPX files
  • net.sf.mpxj.mspdi.MSPDIReader: reads Microsoft MSPDI (XML) files
  • net.sf.mpxj.mpd.MPDReader: reads Microsoft MPD files
  • net.sf.mpxj.planner.PlannerReader: reads Planner (XML) files
  • net.sf.mpxj.primavera.PrimaveraDatabaseReader: reads from a Primavera database
  • net.sf.mpxj.primavera.PrimaveraPMFileReader: reads Primavera PM XML files
  • net.sf.mpxj.primavera.PrimaveraXERFileReader: reads Primavera XER files

A similar arrangement exists for the writer classes:

  • net.sf.mpxj.mpx.MPXWriter: writes Microsoft MPX files
  • net.sf.mpxj.mspdi.MSPDIWriter: writes Microsoft MSPDI (XML) files
  • net.sf.mpxj.planner.PlannerWriter: writes Planner (XML) files

MPXJ is written and maintained in Java, however this is no barrier to using its functionality in .Net. IKVM.NET allows MPXJ to be used from any .Net programming language without having to be aware that the original code was written in Java. IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. It includes the following components:

  • A Java Virtual Machine implemented in .NET
  • A .NET implementation of the Java class libraries
  • Tools that enable Java and .NET interoperability

In order to use this library you can use the following steps:

  • Add a reference for MPXJ.dll and all necessary DLLs (provided DLLs)
  • We need to create a class EnumerableCollection to wrap an IKVM collection to allow enumeration

public class EnumerableCollection

{

    public EnumerableCollection(Collection collection)

    {

        m_collection = collection;

    }

    public IEnumerator GetEnumerator()

    {

        return new Enumerator(m_collection);

    }

    private Collection m_collection;

}

public class Enumerator : IEnumerator

{

    public Enumerator(Collection collection)

    {

        m_collection = collection;

        m_iterator = m_collection.iterator();

    }

    public object Current

    {

        get

        {

            return m_iterator.next();

        }

    }

    public bool MoveNext()

    {

        return m_iterator.hasNext();

    }

    public void Reset()

    {

        m_iterator = m_collection.iterator();

    }

    private Collection m_collection;

    private Iterator m_iterator;

}

  1. Import the following namespaces:
     

    using net.sf.mpxj;

    using net.sf.mpxj.reader;

    using net.sf.mpxj.writer;

    using net.sf.mpxj.mpp;
     

  2. Create an object of "ProjectReader" and "ProjectFile" classes as in the following:

    ProjectReader reader = new MPPReader();
    ProjectFile projectObj = reader.read(<FileName>);
     
  3. For easy enumeration in the collection, we can create a method "ToEnumerable" as in the following:

    private static EnumerableCollection ToEnumerable(Collection javaCollection)

    {

          return new EnumerableCollection(javaCollection);

    }
     

  4. Now if we want to access tasks, subtasks, calendars and resources from a MPP file then we need to use methods using a ProjectFile object.

    A. Tasks
     

    foreach (Task task in ToEnumerable(projectObj.getAllTasks()))

    {

        Console.WriteLine("Task: " + task.getName() + " ID=" + task.getID() + " Unique ID=" + task.getUniqueID());

    }

    B. Resources

    foreach (Resource resource in ToEnumerable(projectObj.getAllResources())

    {

        Console.WriteLine("Resource: " + resource.getName() + "(Unique ID=" + resource.getUniqueID() + ")");

    }

    C. SubTasks

    foreach (Task child in ToEnumerable(task.getChildTasks()))

    {

        Console.WriteLine(indent + "Task: " + child.getName());

        listHierarchy(child, indent + " ");

    }

    D. Access Resource and Task by ID

    • Resource r = project.getResourceByUniqueID(Integer.valueOf(99));
    • Task t = project.getTaskByUniqueID(Integer.valueOf(99));

    E. Calendar

    ProjectCalendar defaultCalendar = projectObj.getCalendar();
    ProjectCalendar taskCalendar = task.getCalendar();
     
  5. There are many methods provided by the MPXJ library you can use depending on your requirements.
  6. Now if you want to create a MPP file, use following steps:

    A. Create Source ProjectFile Object

    • ProjectReader reader = new MPPReader();
    • ProjectFile sourceObj = reader.read(<SourceFileName>);

    B. Create object of "ProjectWriter" class

    ProjectWriter writer = ProjectWriterUtility.getProjectWriter(<outputFile Name>);
    writer.write(sourceObj, <outputFile Name>);

Reference(s)

Up Next
    Ebook Download
    View all
    Learn
    View all