将xml文件绑定到ObjectListview [英] Binding xml file to ObjectListview
本文介绍了将xml文件绑定到ObjectListview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的xml文件,如下所示:
I have a simple xml file that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Tracks>
<Track>
<Name>Bye Bye Bye</Name>
<Album>No Strings Attached</Album>
<Artist>'N Sync</Artist>
<Genre>Teen Pop</Genre>
<Year>2000</Year>
<Duration>00:03:20.6640000</Duration>
<Location>\\psf\Home\Music\iTunes\iTunes Media\Music\'N Sync\No Strings Attached\01 Bye Bye Bye.mp3</Location>
</Track>
<Track>
我想将其绑定到 ObjectListview .任何人有任何简单的想法吗?
I would like to bind it to an ObjectListview. Anyone has any simple idea?
推荐答案
- 创建一个代表对象的类.
- 将XML反序列化为类.
- 使用填充的类填充集合(例如数组或通用列表).
- 确保ObjectListView具有设置了
AspectName
的适当列. - 调用
ObjectListView.SetObjects()
将其绑定到集合.
- Create a class that represents the object.
- Deserialize the XML to the class.
- Populate a collection such as an array or generic list with the populated classes.
- Ensure that the ObjectListView has the appropriate columns with
AspectName
s set. - Call
ObjectListView.SetObjects()
to bind it to the collection.
一个粗略的例子:
StreamReader sr = new StreamReader(Path.Combine(XMLFilePath, XMLFileName));
XmlSerializer x = new XmlSerializer(typeof(ClassTrack));
ClassTrack MyTrack = (ClassTrack)x.Deserialize(sr);
// Deserialize other XML as necessary
List<ClassTrack> TrackCollection = new List<ClassTrack>();
TrackCollection.Add(MyTrack);
// Add other MyTrack objects to collection
olvTrackList.SetObjects(TrackCollection);
这篇关于将xml文件绑定到ObjectListview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文