listview项目c#创建html文件 [英] listview items c# create html file
问题描述
我想创建.html文件,我有一些代码,创建html文件,但他只打印列表视图的第一项,但我也需要子项目..
继承人代码:
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();
}
当我添加这样的项目时:
http://oi45.tinypic.com/ 2hwd2cj.jpg [ ^ ]
当我打开我的html文件时,通过点击另一个代码来创建的html文件:
http://oi49.tinypic.com/34hifys.jpg [ ^ ]
要添加什么来显示所有listview项目,我需要foreach来进行子项目吗?谢谢!
尝试 item.SubItems [0] .Text
。更改所需特定子项的索引。
两件事完全错误。
这不是使用XML的正确方法。以下是我可以使用的方法概述:
- 使用
System.Xml.XmlDocument
类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。- 使用类
System.Xml.XmlTextWriter
和System.Xml.XmlTextReader
;这是最快的阅读方式,特别是你需要跳过一些数据。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。- 使用类
系统.Xml.Linq.XDocument
;这是类似于XmlDocument
的最合适的方式,支持LINQ to XML Programming。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。
现在,关于ListViewItem
。你想念它的结构。ListViewItem
的单个实例表示视图的单行。您的item.Text
表示唯一显示在最左侧单元格中的文本。右边的细胞怎么样?它们由你完全错过的子项表示。您需要在每个列表视图项下迭代它们:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.subitems.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/1x4396ba.aspx [ ^ ]。
类似
ListView myListView = new ListView();
// ...
< span class =code-keyword> foreach (ListViewItem item in myListView.Items){
// add item.Text here
foreach (ListViewItem.ListViewSubItem subitem item.SubItems)
// add每个子项目....
}
-SA
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:
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!
Try item.SubItems[0].Text
. Change the index for the particular subitem(s) you want.
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:
- 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[^].- Use the classes
System.Xml.XmlTextWriter
andSystem.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[^].- Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that ofXmlDocument
, 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 theListViewItem
. You miss its structure. A single instance ofListViewItem
represents a single row of your view. Youritem.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
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
这篇关于listview项目c#创建html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!