C# 将xml文件加载到treelistview中 共享您的xml。共享您的xml。 CarCollection cars = null; string path = @"F:\CustomDictionary.x

C# 将xml文件加载到treelistview中 共享您的xml。共享您的xml。 CarCollection cars = null; string path = @"F:\CustomDictionary.x,c#,.net,objectlistview,treelistview,C#,.net,Objectlistview,Treelistview,将xml文件加载到treelistview中 共享您的xml。共享您的xml。 CarCollection cars = null; string path = @"F:\CustomDictionary.xml"; XmlSerializer serializer = new XmlSerializer(typeof(CarCollection)); StreamReader reader =

将xml文件加载到treelistview中
共享您的xml。共享您的xml。
            CarCollection cars = null;
            string path = @"F:\CustomDictionary.xml";

            XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));

            StreamReader reader = new StreamReader(path);
            cars = (CarCollection)serializer.Deserialize(reader);

            List<CarCollection> TrackCollection = new List<CarCollection>();
            TrackCollection.Add(cars);

            // model is the currently queried object, we return true or false according to the amount of children we have in our MyClasses List
            treeListView1.CanExpandGetter = model => ((CarCollection)model).
                                                          Car.Count() > 0;
            // We return the list of MyClasses that shall be considered Children.
            treeListView1.ChildrenGetter = delegate(object x) { return ((CarCollection)x).Car; };

            treeListView1.SetObjects(TrackCollection);

    [Serializable()]
    public class Car
    {
        [System.Xml.Serialization.XmlElement("StockNumber")]
        public string StockNumber { get; set; }

        [System.Xml.Serialization.XmlElement("Make")]
        public string Make { get; set; }

        [System.Xml.Serialization.XmlElement("Model")]
        public string Model { get; set; }
    }

    [Serializable()]
    [System.Xml.Serialization.XmlRoot("CarCollection")]
    public class CarCollection
    {
        [XmlArray("Cars")]
        [XmlArrayItem("Car", typeof(Car))]
        public Car[] Car { get; set; }
    }
<?xml version="1.0" encoding="utf-8"?>
<CarCollection>
  <Cars>
    <Car>
      <StockNumber>1020</StockNumber>
      <Make>Nissan</Make>
      <Model>Sentra</Model>
    </Car>
    <Car>
      <StockNumber>1010</StockNumber>
      <Make>Toyota</Make>
      <Model>Corolla</Model>
    </Car>
    <Car>
      <StockNumber>1111</StockNumber>
      <Make>Honda</Make>
      <Model>Accord</Model>
    </Car>
  </Cars>
</CarCollection>