Binding xml file to ObjectListview

和自甴很熟 提交于 2020-01-06 14:58:04

问题


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>

I would like to bind it to an ObjectListview. Anyone has any simple idea?


回答1:


  1. Create a class that represents the object.
  2. Deserialize the XML to the class.
  3. Populate a collection such as an array or generic list with the populated classes.
  4. Ensure that the ObjectListView has the appropriate columns with AspectNames set.
  5. Call ObjectListView.SetObjects() to bind it to the collection.

Rough example:

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


来源:https://stackoverflow.com/questions/8855773/binding-xml-file-to-objectlistview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!