Editing/Writing mpp file on c# using mpxj

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

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.

  1. ProjectReader reader = new MPPReader();
  2. ProjectWriter writer = new MPXWriter();
  3. private ProjectFile project;
  4. public ProjectFile OpenReaderProject()
  5. {
  6. string filePath = "Files/Prova file1.mpp";
  7. try
  8. {
  9. project = reader.read(filePath);
  10. }
  11. catch (MPXJException ex)
  12. {
  13. Console.WriteLine("Errore durante l'apertura del file .mpp: " + ex.Message);
  14. }
  15. return project;
  16. }
  17. public ProjectFile OpenWriterProject()
  18. {
  19. string filePath = "Files/Prova file1.mpp";
  20. try
  21. {
  22. writer.write(project, filePath);
  23. }
  24. catch (MPXJException ex)
  25. {
  26. Console.WriteLine("Errore durante la scrittura del file .mpp: " + ex.Message);
  27. }
  28. return project;
  29. }

This is the write and reading part.

  1. public void AddTask()
  2. {
  3. var project = OpenReaderProject();
  4. var task = project.addTask();
  5. task.setName(java.lang.String.valueOf("Attività di esempio"));
  6. var taskDto = _mapper.Map<TaskDto>(task);
  7. OpenWriterProject();
  8. }

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.

  1. ProjectReader reader = new MPPReader();
  2. ProjectWriter writer = new MPXWriter();
  3. private ProjectFile project;
  4. public ProjectFile OpenReaderProject()
  5. {
  6. string filePath = &quot;Files/Prova file1.mpp&quot;;
  7. try
  8. {
  9. project = reader.read(filePath);
  10. }
  11. catch (MPXJException ex)
  12. {
  13. Console.WriteLine(&quot;Errore durante l&#39;apertura del file .mpp: &quot; + ex.Message);
  14. }
  15. return project;
  16. }
  17. public ProjectFile OpenWriterProject()
  18. {
  19. string filePath = &quot;Files/Prova file1.mpp&quot;;
  20. try
  21. {
  22. writer.write(project, filePath);
  23. }
  24. catch (MPXJException ex)
  25. {
  26. Console.WriteLine(&quot;Errore durante la scrittura del file .mpp: &quot; + ex.Message);
  27. }
  28. return project;
  29. }

This is the write and reading part.

  1. public void AddTask()
  2. {
  3. var project = OpenReaderProject();
  4. var task = project.addTask();
  5. task.setName(java.lang.String.valueOf(&quot;Attivit&#224; di esempio&quot;));
  6. var taskDto = _mapper.Map&lt;TaskDto&gt;(task);
  7. OpenWriterProject();
  8. }`

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 对象,像这样:

  1. ProjectFile project;
  2. string filePath = "Files/Prova file1.mpp";
  3. public ProjectFile OpenReaderProject()
  4. {
  5. ProjectReader reader = new MPPReader();
  6. project = reader.read(filePath);
  7. return project;
  8. }
  9. public ProjectFile OpenWriterProject()
  10. {
  11. ProjectWriter writer = new MPXWriter();
  12. writer.write(project, filePath);
  13. return project;
  14. }
英文:

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:

  1. ProjectFile project;
  2. string filePath = &quot;Files/Prova file1.mpp&quot;;
  3. public ProjectFile OpenReaderProject()
  4. {
  5. ProjectReader reader = new MPPReader();
  6. project = reader.read(filePath);
  7. return project;
  8. }
  9. public ProjectFile OpenWriterProject()
  10. {
  11. ProjectWriter writer = new MPXWriter();
  12. writer.write(project, filePath);
  13. return project;
  14. }

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:

确定