LIDOR SYSTEMS

Advanced User Interface Controls and Components

Load/Save Partial Data to XML files in ListView .NET

Created: 01 September 2013

Although we can serialize entire content of ListView control in XML files (including items, their color and format styles), in some cases is better to load or save partial data of ListView control to some XML file.

We can do this by using the built-in methods for serialization found in IntegralUI ListView control. These methods allow you to save only one or a subset of items to an XML file, memory streams or databases. Later we can retrieve this data and choose whether we want to add them to the ListView or completely replace the ListView content.

To demonstrate this at first we will save some partial data to an XML files. In this demonstration we are using only data serialization. The appearance and layout settings from color and format styles are excluded.

Download Sample - ListView Partial Data Serialization in XML Files

In order to set the same alignment for all columns, we need to use ColumnFormatStyle property. This style allows as aligning of all columns at the same time and independently for column header, body and footer. For example:

ListViewSerializer serializer = new ListViewSerializer();

 

// We are saving only the currently selected item to an XML file

if (rbOne.Checked)

serializer.SaveToXml(this.listView1, this.listView1.SelectedItem, saveDlg.FileName, SerializationType.Data);

// We are saving all currently selected items to an XML file

else

serializer.SaveToXml(this.listView1, this.listView1.SelectedItems, saveDlg.FileName, SerializationType.Data);

Dim serializer As New ListViewSerializer()

 

' We are saving only the currently selected item to an XML file

If rbOne.Checked Then

serializer.SaveToXml(Me.listView1, Me.listView1.SelectedItem, saveDlg.FileName, SerializationType.Data)

Else

' We are saving all currently selected items to an XML file

serializer.SaveToXml(Me.listView1, Me.listView1.SelectedItems, saveDlg.FileName, SerializationType.Data)

End If

Related: XML Serialization of Custom Color Schemes

Now that we have some data saved, whenever we load it back from XML files, we can decide whether we want to add the stored items to the end of items collection in target ListView or completely replace the ListView content.

// Suspend the control layout from updating to increase performance

this.listView1.SuspendUpdate();

 

ListViewSerializer serializer = new ListViewSerializer();

if (rbAdd.Checked)

serializer.LoadFromXml(this.listView1, openDlg.FileName, SerializationAction.Update);

else

serializer.LoadFromXml(this.listView1, openDlg.FileName, SerializationAction.Replace);

 

// Resume the layout calculations and update the control

this.listView1.ResumeUpdate();

' Suspend the control layout from updating to increase performance

Me.listView1.SuspendUpdate()

 

Dim serializer As New ListViewSerializer()

If rbAdd.Checked Then

serializer.LoadFromXml(Me.listView1, openDlg.FileName, SerializationAction.Update)

Else

serializer.LoadFromXml(Me.listView1, openDlg.FileName, SerializationAction.Replace)

End If

 

' Resume the layout calculations and update the control

Me.listView1.ResumeUpdate()

You can downlaod complete sample project written in C# and VB from here: ListView Partial Data Serialization

Newsletter


Sign-up to our newsletter and you will receive news on upcoming events, latest articles, samples and special offers.
Name: Email: *
*By checking this box, I agree to receive a newsletter from Lidor Systems in accordance with the Privacy Policy. I understand that I can unsubscribe from these communications at any time by clicking on the unsubscribe link in all emails.