Version

Saving a Project Plan to a Microsoft Project XML File (xamGantt)

Topic Overview

Purpose

This topic explains how to save a xamGantt project plan in a Microsoft Project XML file.

Required background

The following topic is a prerequisite to understanding this topic:

Topic Purpose

This topic describes how you can add the xamGantt control to a page.

Saving a Project Plan to an MS Project XML File

Introduction

The procedure describes how to save a xamGantt project plan to an MS Project XML file.

Note
Note

When saving the project plan, the xamGantt control only supports the MS Project 2010 file format.

Preview

The following screenshot is a preview of the results.

xamGantt Saving a Project to XML.png

Overview

Following is a conceptual overview of the process:

  1. Add a xamGantt control in XAML

  2. Add an open dialog box button for saving the project plan as XML file

  3. Add required references in code-behind

  4. Handle the button click event

  5. Save the project plan as MS Project XML file

Steps

The following steps demonstrate saving a xamGantt project plan to an XML file.

  1. Add a xamGantt control in XAML

    Add the xamGantt control to the first row of a Grid container:

    In XAML:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ig:XamGantt x:Name="gantt" />
        <!-- Add other controls here -->
    </Grid>
  1. Add a button for showing the save to XML file dialog

    Add a Button control in the second row of the Grid container:

    In XAML:

     <Button x:Name="Btn_SaveProject"
            Grid.Row="1"
            Content="Save XML Project File"
            Click="Btn_SaveProject_Click"/>
  1. Add required references in code-behind

    Add the following using or Imports declarations in the code-behind:

    In C#:

    using System;
    using System.IO;
    using System.Windows;
    using Infragistics;
    using Infragistics.Controls.Schedules;
    using Microsoft.Win32;

    In Visual Basic:

    Imports System.IO
    Imports Infragistics
    Imports Infragistics.Controls.Schedules
    Imports Microsoft.Win32
  1. Handle the button click event

    Handle the button click event to save the xamGantt project plan to an MS Project XML file.

  1. Save the xamGantt project to an MS Project XML file

    Use the Project’s SaveAsProjectXml method to save a project plan to a stream.

    Note
    Note

    The Loading a Project Plan from an MS Project XML File topic demonstrates how to load sample data.

Code Examples

Code examples summary

The following table lists the code examples included in this topic.

Example Description

The example code uses the SaveFileDialog class to save a xamGantt’s project plan to an MS Project XML file.

The example code saves the xamGantt’s project plan to an MS Project XML file.

Code Example: Handle the Button Click Event to Save the Project Plan as XML File

Description

The example code uses the SaveFileDialog class to save a xamGantt’s project plan to an MS Project XML file.

Code

In C#:

using Microsoft.Win32;
private void Btn_SaveProject_Click(object sender, RoutedEventArgs e)
{
    SaveFileDialog dialog = new SaveFileDialog();
    dialog.InitialDirectory = "c:\\";
    dialog.Filter = "MS Project XML (.xml)|*.xml|All Files (*.*)|*.* ";
    bool? isSaved = dialog.ShowDialog();
    if (isSaved == true)
    {
        try
        {
            using (Stream stream = dialog.OpenFile())
            {
                SaveProjectToStream(stream);
                stream.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

In Visual Basic:

Imports Microsoft.Win32
Private Sub Btn_SaveProject_Click(sender As Object, e As RoutedEventArgs)
    Dim dialog As New SaveFileDialog()
    dialog.InitialDirectory = "c:\"
    dialog.Filter = "MS Project XML (.xml)|*.xml|All Files (*.*)|*.* "
    Dim isSaved As System.Nullable(Of Boolean) = dialog.ShowDialog()
    If isSaved = True Then
        Try
            Using stream As Stream = dialog.OpenFile()
                SaveProjectToStream(stream)
                stream.Close()
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End If
End Sub

Code Example: Saving a xamGantt’s Project Plan to an MS Project XML File

Description

The following code examples demonstrate how to save xamGantt project plans to an MS Project XML file in both C# and Visual Basic.

Code

In C#:

private void SaveProjectToStream(Stream stream)
{
    // obtain the xamGantt Project
    var project = this.gantt.Project;
    project.SaveAsProjectXml(stream);
}

In Visual Basic:

Private Sub SaveProjectToStream(stream As Stream)
    ' obtain the xamGantt Project
    Dim project = Me.gantt.Project
    project.SaveAsProjectXml(stream)
End Sub

Related Topics

The following topics provide additional information related to this topic.

Topic Purpose

This topic gives an overview of xamGantt control data binding.

This topic describes how the xamGantt control is bound to data via Project property.

This topic describes how the xamGantt control is bound to an arbitrary tasks collection via ListBackedProject.

This topic describes how an already created project plan saved in a Microsoft Project™ 2010 XML file is loaded in the xamGantt control.