Customizing Project XML serialization with Gantt Chart Light Library

If you use a GanttChartDataGrid component and you use objects inheriting from the default GanttChartItem class to provide CustomField properties of your own (possibly showing them as well into the grid’s Columns) you may want to export/import the custom field values to/from Project XML formatted strings/files as well.

In this case you will need to use a ProjectXmlSerializer instance (instead of SaveProjectXml method of the component) and set up event handlers for GanttChartItemSaving (for serialization) and GanttChartItemLoading events (for deserialization) and inject custom logic to save/load custom values to/from the Project XML elements yourself.

For example (assuming CustomTaskItem inherits from GanttChartItem and defines a CustomField property of type string):

// Save to file/Get XML string:
var serializer = new GanttChartDataGrid.ProjectXmlSerializer(GanttChartDataGrid);
serializer.GanttChartItemSaving += Serializer_GanttChartItemSaving;
var xml = serializer.GetXml();
// use xml

private void Serializer_GanttChartItemSaving(object sender, GanttChartView.ProjectXmlSerializer.GanttChartItemSavingEventArgs e)
{
  var customItem = e.GanttChartItem as CustomTaskItem;
  var xml = e.OutputXml;
  var element = XElement.Parse(xml);
  element.Add(new XElement(XName.Get("CustomField", "custns"), customItem.CustomField));
  xml = element.ToString();
  e.OutputXml = xml;
}
// Load from string/file:
var xml = …; //prepare XML string
var serializer = new GanttChartDataGrid.ProjectXmlSerializer(GanttChart);
serializer.GanttChartItemLoading += Serializer_GanttChartItemLoading;
serializer.Load(xml);

private void Serializer_GanttChartItemLoading(object sender, GanttChartView.ProjectXmlSerializer.GanttChartItemLoadingEventArgs e)
{
  var item = e.GanttChartItem;
  var element = e.SourceElement;
  var customItem = new CustomTaskItem
  {
    Content = item.Content,
    Start = item.Start,
    …
    CustomField = element.Element(XName.Get("CustomField", "custns"))?.Value
  };
  e.GanttChartItem = customItem;
}
Advertisement

About DlhSoft Team

DlhSoft is a software development company focused on building high quality and innovative solutions using state-of-the-art technologies for customers worldwide. Our activity includes designing and developing general purpose software applications, components and tools, and customized software solutions upon request. DlhSoft team is highly experienced and fully dedicated to software development. Additionally, one of the most important things we rely on is the continuous communication with the customer.
This entry was posted in Development Components and tagged , , , , , . Bookmark the permalink.

Add a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s