C#:在XML文件中保存ListView项 [英] C# : Save ListView Items in XML file
问题描述
您好,
可以在xml文件中保存listView项目吗?
(每个colomun代表节点)
然后我们如何保存节点属性
for exp:
< 类 id = 1 , 名称 = 计算机科学 >
< 主题 id = 1 , title = Java >
< 章 id = 1 / >
< / Subject >
< 主题 <温泉n class =code-attribute> id = 2 , title = c# >
< 章 id = 1 / >
< 章节 id = 2 / >
< 章节 id = 3 / >
< / Subject >
< 主题 id = 3 , title = UML / >
< / Class >
然后我们在你的listview colomun中:class,subject,chapter,然后是行中属性的值,我想选择一些行并将它们写在xml文件中?这是可能的 ?如果是的话怎么样?
初学者......小一:(
你应该使用System.Xml.Serialization命名空间,用于序列化和反序列化listview对象。请参阅 MSDN [ ^ ]了解更多信息。
使用System.Xml.Serialization;
///假设myListView是你的listview对象
public void SaveToXml(string myXmlFilePath){
XmlSerializer serializer = new XmlSerializer(typeof(ListViewItemCollection) );
使用(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);
};
};
}
当然也有可能也不太难实现。
// 您需要遍历ListView中的行,例如ListView lv。
foreach (ListViewItem item in lv.Items)
// 如果您只想保存一些项目,请使用lv.SelectedItems
{
foreach (SubListViewItem subItem in item.SubItems)
{
// 为每个子项创建一个XElement。
}
}
// 将数据保存到文件
我建议使用XDocument和XElement。
这是MSDN的链接以获得进一步的帮助:XDocument类概述 [ ^ ]
那里有一些非常好的例子。
[更新]
一个小例子来帮助你>
// 初始化包含3列数据的列表视图
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( 根跨度>);
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);
// 要添加属性,请使用XAttributes
}
xeRoot。加入(xeRow);
}
// 我将保存文件留给您
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>
<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 :(
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); }; }; }
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.
[Update]
A little example to help you along
// 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
这篇关于C#:在XML文件中保存ListView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!