Click here to Skip to main content
15,500,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,

It's is possible to save listView Items in xml file ?

(each colomun represent node )
Then how we can save node attributes also

for exp :
<Class  id="1",  name="Computer Science">
  <Subject  id="1" , title ="Java">
   <chapter  id="1" />
  <Subject id="2" , title ="c#" >
<chapter  id="1" />
<chapter  id="2" />
<chapter  id="3" />
  <Subject   id="3" , title ="UML"/>

Then we have in ou our listview colomun : class , subject , chapter , then value of attributes in rows and i want to select some rows and write them in xml file ? It's possible ? if yes How ?

Am beginner ... small one :(

You should use the System.Xml.Serialization namespace to serialize and deserialize your listview object. See MSDN[^] for more information.

using System.Xml.Serialization;

/// assuming  myListView is your listview object 

public void SaveToXml(string myXmlFilePath){

       XmlSerializer serializer = new XmlSerializer(typeof(ListViewItemCollection));

       using (FileStream stream = File.OpenWrite(myXmlFilePath)){
       serializer.Serialize(stream, myListView.Items);}

public void LoadFromXML(string myXmlFilePath){
              if (File.Exists( myXmlFilePath))
                             XmlSerializer serializer = new XmlSerializer(typeof(ListViewItemCollection));
                              using (FileStream stream = File.OpenRead( myXmlFilePath))
                                     myListView.Items = (ListViewItemCollection)serializer.Deserialize(stream);
Share this answer
Member 11192477 30-Oct-14 11:20am     CRLF
Thank'u am using it , but I have an error with ListViewItemCollection . I dont know why , but it's tell me that type or name space 'ListViewItemCollection' is not found , a using directive or reference needed ?
Member 14196931 5-Aug-20 4:53am     CRLF
var lst =(ListViewItemCollection)serializer.Deserialize(stream); myListView.Items.AddRange(lst);
Of course it is possible and not too difficult to achieve either.

// You need to loop through the rows in the ListView, e.g. ListView lv.
foreach (ListViewItem item in lv.Items) 
// Use lv.SelectedItems if you only want to save a few items
    foreach (SubListViewItem subItem in item.SubItems)
        // Create an XElement for each sub item.
// Save the data to file

I recommend to use XDocument and XElement.
Here is a link to MSDN for further help: XDocument Class Overview[^]
There are some pretty good examples there.

A little example to help you along
// Initialize a listview with 3 columns with data
ListViewItem row1 = listView1.Items.Add("Row1");

ListViewItem row2 = listView1.Items.Add("Row2");

ListViewItem row3 = listView1.Items.Add("Row3");

ListViewItem row4 = listView1.Items.Add("Row4");

XElement xeRoot = new XElement("Root");
foreach (ListViewItem item in listView1.SelectedItems)
    XElement xeRow = new XElement(item.Text);
    foreach (ListViewItem.ListViewSubItem subItem in item.SubItems)
        XElement xeCol = new XElement(subItem.Text);
        // To add attributes use XAttributes

// I leave the saving to file to you
Share this answer
Member 11192477 30-Oct-14 11:26am     CRLF
It's not easy too for a starter :( Yes i want to save only a few items , how i can use ls.selecteditems ? thanks for link , i will see it Now , hope that i find an exp
Member 11192477 30-Oct-14 11:40am     CRLF
in the exp , we create a XElement for each xml node , Maybe my question is stupide but i don't have choose :D : in listView , how we can know how many rows/items will be selected ? and we can't create an Xelement for every node and attribute selected ... it's not clear for me :(
George Jonsson 30-Oct-14 11:55am    
See my updated example

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900