Editing/Writing mpp file on c# using mpxj

huangapple go评论71阅读模式
英文:

Editing/Writing mpp file on c# using mpxj

问题

I'm trying to view and edit Tasks on a mpp file using mpxj on c#. I wrote some lines and tring to write a file that is already complete adding a new Task.

But when the code arrives to the write part, it create the file in the path I meant but the file is empty and if I try to read that file it give me the System.NullReferenceException : Object reference not set to an instance of an object error. It's the first time code with this mpxj library.

ProjectReader reader = new MPPReader();
ProjectWriter writer = new MPXWriter();
private ProjectFile project;

public ProjectFile OpenReaderProject()
{
    string filePath = "Files/Prova file1.mpp";

    try
    {
        project = reader.read(filePath);
    }
    catch (MPXJException ex)
    {
        Console.WriteLine("Errore durante l'apertura del file .mpp: " + ex.Message);
    }
    return project;
}

public ProjectFile OpenWriterProject()
{
    string filePath = "Files/Prova file1.mpp";

    try
    {
        writer.write(project, filePath);
    }
    catch (MPXJException ex)
    {
        Console.WriteLine("Errore durante la scrittura del file .mpp: " + ex.Message);
    }
    return project;
}

This is the write and reading part.

public void AddTask()
{
    var project = OpenReaderProject();

    var task = project.addTask();
    task.setName(java.lang.String.valueOf("Attività di esempio"));
    var taskDto = _mapper.Map<TaskDto>(task);
    OpenWriterProject();
}

This is the add task function and calling the OpenWriterProject.
What's wrong with it? Feel free to answer me Editing/Writing mpp file on c# using mpxj Thanks

英文:

I'm trying to view and edit Tasks on a mpp file using mpxj on c#. I wrote some lines and tring to write a file that is already complete adding a new Task.

But when the code arrives to the write part, it create the file in the path I meant but the file is empty and if I try to read that file it give me the System.NullReferenceException : Object reference not set to an instance of an object error. It's the first time code with this mpxj library.

 ProjectReader reader = new MPPReader();
 ProjectWriter writer = new MPXWriter();
 private ProjectFile project;

 public ProjectFile OpenReaderProject()
    {
        string filePath = &quot;Files/Prova file1.mpp&quot;;

        try
        {
            project = reader.read(filePath);
        }
        catch (MPXJException ex)
        {
            Console.WriteLine(&quot;Errore durante l&#39;apertura del file .mpp: &quot; + ex.Message);
        }
        return project;
    }

    public ProjectFile OpenWriterProject()
    {
        string filePath = &quot;Files/Prova file1.mpp&quot;;

        try
            {
            writer.write(project, filePath);
            }
            catch (MPXJException ex)
            {
                Console.WriteLine(&quot;Errore durante la scrittura del file .mpp: &quot; + ex.Message);
            }
        return project;
    }

This is the write and reading part.

 public void AddTask()
    {
        var project = OpenReaderProject();

        var task = project.addTask();
        task.setName(java.lang.String.valueOf(&quot;Attivit&#224; di esempio&quot;));
        var taskDto = _mapper.Map&lt;TaskDto&gt;(task);
            OpenWriterProject();

    }`

This is the add task function and calling the OpenWriterProject.
What's wrong with it? Feel free to answer me Editing/Writing mpp file on c# using mpxj Thanks

答案1

得分: 1

你需要在使用写入器之前关闭阅读器。我建议你在使用它们之前仅创建 readerwriter 对象,像这样:

ProjectFile project;
string filePath = "Files/Prova file1.mpp";

public ProjectFile OpenReaderProject()
{
    ProjectReader reader = new MPPReader();
    project = reader.read(filePath);
    return project;
}

public ProjectFile OpenWriterProject()
{
    ProjectWriter writer = new MPXWriter();
    writer.write(project, filePath);
    return project;
}
英文:

You will have to close the reader before using the writer. I suggest that you only create the reader and writer objects just before you use them, like this:

ProjectFile project;
string filePath = &quot;Files/Prova file1.mpp&quot;;

public ProjectFile OpenReaderProject()
{
    ProjectReader reader = new MPPReader();
    project = reader.read(filePath);
    return project;
}

public ProjectFile OpenWriterProject()
{
    ProjectWriter writer = new MPXWriter();
    writer.write(project, filePath);
    return project;
}

huangapple
  • 本文由 发表于 2023年7月3日 21:52:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定