问题
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:
- 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 AspectNames set.
- 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