将xml文件绑定到ObjectListview

时间:2012-01-13 19:02:34

标签: xml listview data-binding objectlistview

我有一个简单的xml文件,如下所示:

<?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。任何人都有任何简单的想法?

1 个答案:

答案 0 :(得分:2)

  1. 创建一个代表该对象的类。
  2. Deserialize the XML to the class
  3. 使用填充的类填充数组或通用列表等集合。
  4. Ensure that the ObjectListView has the appropriate columns with AspectNames set
  5. 调用ObjectListView.SetObjects()将其绑定到集合。
  6. 粗略的例子:

    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);