如何在C#中一个ListView写XML文件 [英] How to write XML file from a ListView in C#
问题描述
我遇到像
我需要从这个ListView控件的数据存储到一个XML文件。
I need to store the data from this ListView to an Xml File.
因此XML文件会像
So the xml file will be like
<root>
<Child Name ="A1" val1="1" val2="0"/>
<Child Name ="A2" val1="1" val2="2"/>
<Child Name ="A3" val1="1" val2="3"/>
<Child Name ="A4" val1="1" val2="4"/>
<Child Name ="A5" val1="1" val2="5"/>
<Child Name ="A6" val1="6" val2="0"/>
<Child Name ="A7" val1="7" val2="0"/>
</root>
如果数据被存储在某些列表
或词典
,然后我知道要做到这一点使用XML LINQ
if the data is stored in some List
or Dictionary
, then i know to do this using XML to LINQ
但我怎么做到这一点的一个ListView
But how do i do this from a ListView
XDocument XD=new XDocument(new XElement("root",........// what i have to do here.......
请帮我做到这一点...
Please help me to do this...
在此先感谢
推荐答案
您可以查询列表视图子项与它的列标题沿:</ P>
You can query the list view subitems along with its column headers:
XDocument document = new XDocument(new XElement("root",
from item in yourListView.Items.Cast<ListViewItem>()
select new XElement("Child",
item.SubItems.Cast<ListViewSubItem>()
.Select((subitem, i) => new XAttribute(
i == 0 ? "Name" : yourListView.Columns[i].Text.ToLower(),
subItem.Text)))));
编辑:由于没有<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.listviewitemcollection.aspx\"相对=nofollow> ListViewItemCollection 也不<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.listviewsubitemcollection.aspx\"相对=nofollow> ListViewSubItemCollection 支持查询操作的开箱,我们需要调用的 演员LT; T&GT;()
才能够使用它们。
Since neither ListViewItemCollection nor ListViewSubItemCollection support query operators out of the box, we need to call Cast<T>()
to be able to use them.
这篇关于如何在C#中一个ListView写XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!