英文:
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 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 = "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 Thanks
答案1
得分: 1
你需要在使用写入器之前关闭阅读器。我建议你在使用它们之前仅创建 reader
和 writer
对象,像这样:
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 = "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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论