Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create .html file, i have some code , that creates html file, but he only print first item of a listview , but i need subitems also..

heres the code:
C#
private void button6_Click(object sender, EventArgs e)
        
            StreamWriter sw = new StreamWriter("C:\\Users\\Nemanja\\Desktop\\index_lv.html");
            sw.WriteLine("<html>");
            sw.WriteLine("<head>");
            sw.WriteLine("</head>");
            sw.WriteLine("<body>");
            foreach (ListViewItem item in listView1.Items)
            {
                sw.WriteLine(item.Text);
            }
            sw.WriteLine("</body>");
            sw.WriteLine("</html>");
            sw.Close();
        }



When i add items like this:

http://oi45.tinypic.com/2hwd2cj.jpg[^]


and when i open my html file which was created by clicking on another button which have code like up:

http://oi49.tinypic.com/34hifys.jpg[^]


What to add to show all listview items, do i need foreach for subitems? Thanks!
Posted

Try item.SubItems[0].Text. Change the index for the particular subitem(s) you want.
 
Share this answer
 
v2
Comments
Abhinav S 12-Mar-13 22:56pm    
Yep. 5.
Two things are totally wrong.

This is not a proper way of working with XML. Here is my overview of the approaches you could use:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


Now, about the ListViewItem. You miss its structure. A single instance of ListViewItem represents a single row of your view. Your item.Text represents the only the text which is shown in a leftmost cell. How about the cells shown on right? They are represented by sub-items you completely miss. You need to iterate them under each list view item:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.subitems.aspx[^],
http://msdn.microsoft.com/en-us/library/1x4396ba.aspx[^].

Something like
C#
ListView myListView = new ListView();

//...

foreach (ListViewItem item in myListView.Items) {
    // add item.Text here
    foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
        // add each subitem....
}


—SA
 
Share this answer
 
v3
Comments
Abhinav S 12-Mar-13 22:55pm    
Nice answer. 5.
Sergey Alexandrovich Kryukov 12-Mar-13 23:12pm    
Thank you, Abhinav.
—SA
[no name] 13-Mar-13 6:12am    
Thanks man, i needed foreach(ListViewItem.ListViewSubitem - > that was my problem and also item.SubItems - > that was also my second problem, i am new to c# so i am learning it... About XML thanks for those links, i am using "System.Xml;" namespace, so thanks again! 5 stars!
Sergey Alexandrovich Kryukov 13-Mar-13 11:43am    
You are welcome.
Good luck, call again.
—SA

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