Binding xml file to ObjectListview

2019-08-27 11:07发布

问题:

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