从项目(WPF)中的XML文件填充Observable集合

时间:2018-08-12 11:33:50

标签: c# .net wpf observablecollection

我的项目中有xml文件

在这里

  <?xml version="1.0"?>
<catalog>
   <car id="1">
      <model>Scoda Fabia</model>
      <year>2011</year>
      <producer>Folkwagen</producer>
      <price>6000</price>
      <owner>Bil Johnson</owner>
      <tel>+5810456455456</tel>
      <mileage>670000</mileage>
      <registered>USA</registered>
      <image>Fabia1.JPG</image>
   </car>
   <car id="2">
      <model>Huindai Getz</model>
      <year>2008</year>
      <producer>Huindai</producer>
      <price>5000</price>
      <owner>Dimitrious Gregorakis</owner>
      <tel>+5810456445456</tel>
      <mileage>120000</mileage>
      <registered>USA</registered>
      <image>hyundai_getz2.jpg</image>
   </car>
   <car id="3">
      <model>Huindai i108</model>
      <year>2014</year>
      <producer>Huindai</producer>
      <price>15000</price>
      <owner>Dex Dexter</owner>
      <tel>+5815556445456</tel>
      <mileage>30000</mileage>
      <registered>Canada</registered>
      <image>hyundaii108.jpg</image>
   </car>
   <car id="4">
      <model>Aveo</model>
      <year>2000</year>
      <producer>Shevrole</producer>
      <price>3500</price>
      <owner>Ivan Ivanov</owner>
      <tel>+5815556445477</tel>
      <mileage>300000</mileage>
      <registered>Mexico</registered>
      <image>aveo.jpg</image>
   </car>
</catalog>

我从中创建了课程,这是课程代码

 [XmlRoot(ElementName = "car")]
public class Car
{
    [XmlElement(ElementName = "model")]
    public string Model { get; set; }
    [XmlElement(ElementName = "year")]
    public string Year { get; set; }
    [XmlElement(ElementName = "producer")]
    public string Producer { get; set; }
    [XmlElement(ElementName = "price")]
    public string Price { get; set; }
    [XmlElement(ElementName = "owner")]
    public string Owner { get; set; }
    [XmlElement(ElementName = "tel")]
    public string Tel { get; set; }
    [XmlElement(ElementName = "mileage")]
    public string Mileage { get; set; }
    [XmlElement(ElementName = "registered")]
    public string Registered { get; set; }
    [XmlElement(ElementName = "image")]
    public string Image { get; set; }
    [XmlAttribute(AttributeName = "id")]
    public string Id { get; set; }
}

[XmlRoot(ElementName = "catalog")]
public class Catalog
{
    [XmlElement(ElementName = "car")]
    public List<Car> Car { get; set; }
}

并为此创建了ViewModel,我在其中定义了可观察的集合和定义方法,以用xml中的数据填充它

public class CarViewModel
{
    public ObservableCollection<List<Car>> car { get; set; }
    public void LoadCars()
    {
        Car = new ObservableCollection<List<Car>>();

        var path = @"xml\CarsDatabase.xml";
        using (TextReader reader = new StreamReader(path))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Catalog));
            return (Catalog)serializer.Deserialize(reader);
        }
    }
}

在LoadCars方法中,我需要用文件中的数据填充汽车可观察的集合,该文件位于项目内部。

如何正确执行此操作? 谢谢您的帮助。

更新

我尝试使用这种方法

public void LoadCars()
    {
        Car = new ObservableCollection<List<Car>>();

        var path = @"xml\CarsDatabase.xml";
        using (TextReader reader = new StreamReader(path))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Catalog));
            return (Catalog)serializer.Deserialize(reader);
        }
    }

但是现在我有错误

  

严重性代码描述项目文件行抑制状态   错误CS0127由于'CarViewModel.LoadCars()'返回void,因此对象关键字DaxxTest C:\ Users \ nemes \ Source \ Repos \ daxx_test \ DaxxTest \ DaxxTest \ DaxxTest \ ViewModels \ CarViewModel.cs 25后面不得包含return关键字。 / p>

1 个答案:

答案 0 :(得分:2)

更改

public ObservableCollection<List<Car>> car { get; set; } 

public ObservableCollection<Car> car { get; set; }

并使用XmlSerializer序列化您的xml信息。查看下面的示例代码:

        public ObservableCollection<Car> cars { get; set; }

        public void LoadCars()
        {

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

            StreamReader reader = new StreamReader("CarsDatabase.xml");
            var catalog = (Catalog)serializer.Deserialize(reader);
            cars = new ObservableCollection<Car>(catalog.Car);
            reader.Close();
        }

        [Serializable()]
        public class Car
        {
            [XmlElement(ElementName = "model")]
            public string Model { get; set; }
            [XmlElement(ElementName = "year")]
            public string Year { get; set; }
            [XmlElement(ElementName = "producer")]
            public string Producer { get; set; }
            [XmlElement(ElementName = "price")]
            public string Price { get; set; }
            [XmlElement(ElementName = "owner")]
            public string Owner { get; set; }
            [XmlElement(ElementName = "tel")]
            public string Tel { get; set; }
            [XmlElement(ElementName = "mileage")]
            public string Mileage { get; set; }
            [XmlElement(ElementName = "registered")]
            public string Registered { get; set; }
            [XmlElement(ElementName = "image")]
            public string Image { get; set; }
            [XmlAttribute(AttributeName = "id")]
            public string Id { get; set; }
        }

        [Serializable()]
        [XmlRootAttribute("catalog", Namespace = "", IsNullable = false)]
        public class Catalog
        {
            [XmlElement(ElementName = "car")]
            public List<Car> Car { get; set; }
        }