简体   繁体   English

如何将 xml 对象反序列化回 class 列表

[英]How to deserialize xml objects back to a class list

I am trying to create a windows form application where I create a list of vehicle class objects in a business class list.我正在尝试创建一个 windows 表单应用程序,其中我在业务 class 列表中创建车辆 class 对象的列表。 When a new Item is added to the list, the file is serialized.当一个新项目被添加到列表中时,该文件被序列化。 I have created a class called Vehicle that contains the xml attributes, a method for adding a new instance of the vehicle class, and a way to serialize the data to an xml file. I have created a class called Vehicle that contains the xml attributes, a method for adding a new instance of the vehicle class, and a way to serialize the data to an xml file. I would like to close the application and reopen it to display this data in a view such as a datagridview.我想关闭应用程序并重新打开它以在数据网格视图等视图中显示这些数据。 The issue I am currently having is that Im not sure how to deserialize the data back into the class list and display it.我目前遇到的问题是我不确定如何将数据反序列化回 class 列表并显示它。 When I exit the application and open it again then try and add new vehicles, it overwrites the current data in the xml file.当我退出应用程序并再次打开它然后尝试添加新车辆时,它会覆盖 xml 文件中的当前数据。

Here is my Business class with my serialization method:这是我的业务 class 和我的序列化方法:

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

And here is the vehicle class:这是车辆 class:

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

Here is the creation of a vehicle:这是车辆的创建:

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

This is what the xml data looks like这就是 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>

How might I deserialize this data back to my VehicleList and display it into a datagridview?我如何将这些数据反序列化回我的VehicleList并将其显示到 datagridview 中?

You just need to put into a dataset and then make table zero the DataSource of a DGV.您只需要放入数据集,然后将零表设为 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);
 
        }
 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM