【问题标题】:How to deserialize xml objects back to a class list如何将 xml 对象反序列化回类列表
【发布时间】:2021-05-28 00:28:53
【问题描述】:

我正在尝试创建一个 Windows 窗体应用程序,在该应用程序中,我在业务类列表中创建了一个车辆类对象列表。当一个新项目被添加到列表中时,该文件被序列化。我创建了一个名为 Vehicle 的类,其中包含 xml 属性、添加车辆类新实例的方法以及将数据序列化到 xml 文件的方法。我想关闭应用程序并重新打开它以在数据网格视图等视图中显示这些数据。我目前遇到的问题是我不确定如何将数据反序列化回类列表并显示它。当我退出应用程序并再次打开它然后尝试添加新车辆时,它会覆盖 xml 文件中的当前数据。

这是我的商务类和我的序列化方法:

using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace VehicleSystem
{
    public class Business
    {
        private static List<Vehicle> _vehicleList = new List<Vehicle>();

        public static List<Vehicle> VehicleList
        {
            get => _vehicleList;
        }

      
        public static void Save()
        {
            XmlSerializer serial = new XmlSerializer(typeof(List<Vehicle>));
            using (FileStream file = new FileStream("C:\\temp\\data.xml", FileMode.Create))
            {
                serial.Serialize(file, VehicleList);
                file.Close();
            }
        }
    }
}

这是车辆类别:

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace VehicleSystem
{
    [XmlType("vehicle")]
    [Serializable]
    public class Vehicle
    {
        private int _cost;
        private int _year;
        private string _make;
        private string _model;
        private string _registration;

        [XmlElement("Registration")]
        public string Registration { get => _registration; set => _registration = value; }
        [XmlElement("Model")]
        public string Model { get => _model; set => _model = value; }
        [XmlElement("Make")]
        public string Make { get => _make; set => _make = value; }
        [XmlElement("Year")]
        public int Year { get => _year; set => _year = value; }
        [XmlElement("Cost")]
        public int Cost { get => _cost; set => _cost = value; }

        public Vehicle(string registration, string model, string make, int year, int cost)
        {
            Registration = registration;
            Model = model;
            Make = make;
            Year = year;
            Cost = cost;
        }
    }
}

这是车辆的创建:

Business.VehicleList.Add(new Vehicle("ABC123","Hilux","Toyota", 1992, 123));
Business.Save();

这就是xml数据的样子

        <?xml version="1.0"?>
    
-<ArrayOfVehicle xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    -<vehicle>
        <Registration>ABC123</Registration>
        <Model>Hilux</Model>
        <Make>Toyota</Make>
        <Year>1992</Year>
        <Cost>123</Cost>
    </vehicle>
</ArrayOfVehicle>

如何将这些数据反序列化回我的 VehicleList 并将其显示到 datagridview 中?

【问题讨论】:

    标签: c# xml serialization datagridview


    【解决方案1】:

    您只需要放入一个数据集,然后将零表作为 DGV 的数据源。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    
    namespace ConsoleApplication189
    {   
        class Program
        {
            const string FILENAME = @"C:\temp\test.xml";
            static void Main(string[] args)
            {
                DataSet ds = new DataSet();
                ds.ReadXml(FILENAME);
     
            }
     
        }
    

    【讨论】:

      猜你喜欢
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 2017-08-20
      相关资源
      最近更新 更多