# Saving and Loading ListView Content

Better ListView: Ultimate .NET ListView replacement control for WinForms (C#, VB.NET)
Download More Info

Better ListView provides methods for storing its content (items with columns and groups) in a file or stream:

- SaveContentBinary(string filename)

- SaveContentBinary(Stream stream)

- SaveContentXml(string filename)

- SaveContentXml(XmlWriter writer)

Analogically, there are methods to retrieving the stored content back:

- LoadContentBinary(string filename)

- LoadContentBinary(Stream stream)

- LoadContentXml(string filename)

- LoadContentXml(XmlReader reader)

Storing Better ListView content in a file is very easy. The Following sample shows storing the content in a binary file:

C#

    // save Better ListView items, columns and groups in a XML file
    this.listView.SaveContentBinary("listview-content.dat");

    // clear content to ensure it is loaded back correctly
    this.listView.Clear();

    // restore content from file
    this.listView.LoadContentBinary("listview-content.dat");

Visual Basic

    ' save Better ListView items, columns and groups in a XML file
    ListView.SaveContentBinary("listview-content.dat")

    ' clear content to ensure it is loaded back correctly
    ListView.Clear()

    ' restore content from file
    ListView.LoadContentBinary("listview-content.dat")

The content can be stored in either binary or XML format. For example, to store the content in a stream as formatted XML, use the following code:

C#

    XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);

    writer.Formatting = Formatting.Indented;

    listView.SaveContentXml(writer);

    writer.Close();

Visual Basic

    Dim writer As New XmlTextWriter(stream, Encoding.UTF8)

    writer.Formatting = Formatting.Indented

    listView.SaveContentXml(writer)

    writer.Close()

Note that in this case it is necessary to have XmlTextReader.WhitespaceHandling property set to None when loading content from formatted XML stream because white spaces need to be skipped during the deserialization process.

Standard Serialization mechanisms are used to store elements and its properties. The methods named above store also mapping between items and their corresponding groups. This cannot be done when simply serializing Groups or Items collection.

When content is loaded into Better ListView, the current content is cleared.

Better ListView: Ultimate .NET ListView replacement control for WinForms (C#, VB.NET)
Download More Info