Click here to Skip to main content
15,883,883 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 :
HTML
<Class  id="1",  name="Computer Science">
  <Subject  id="1" , title ="Java">
   <chapter  id="1" />
  </Subject>
  <Subject id="2" , title ="c#" >
<chapter  id="1" />
<chapter  id="2" />
<chapter  id="3" />
  </Subject>
  <Subject   id="3" , title ="UML"/>
</Class>



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 :(
Posted

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
 
Comments
Member 11192477 30-Oct-14 11:20am    
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    
var lst =(ListViewItemCollection)serializer.Deserialize(stream);
myListView.Items.AddRange(lst);
Of course it is possible and not too difficult to achieve either.

C#
// 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.

[Update]
A little example to help you along
C#
// Initialize a listview with 3 columns with data
ListViewItem row1 = listView1.Items.Add("Row1");
row1.SubItems.Add("Col1_2");
row1.SubItems.Add("Col1_3");

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

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

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

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);
        xeRow.Add(xeCol);
        // To add attributes use XAttributes
    }
    xeRoot.Add(xeRow);
}

// I leave the saving to file to you
 
Share this answer
 
v5
Comments
Member 11192477 30-Oct-14 11:26am    
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    
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