使用模型进行xml序列化/反序列化

时间:2017-12-17 08:01:57

标签: c# xml serialization deserialization models

我想要实现的是XML输出,如:

<cars>
 <car id="0">
  <Type>1</Type>
  <Class>B</Class>
 </car>
 <car id="1">
  <Type>1</Type>
  <Class>B</Class>
 </car>
</cars>

所有汽车数据都是单独的XML文件。以下代码的一部分我可以使用汽车模型将这些数据读入字典:

    public Dictionary<int, ModelCar> Car { get; }

    public Cars()
    {

        Car = new Dictionary<int, ModelCar>();

        foreach (var File in Directory.GetFiles(Folder, "*.xml", SearchOption.TopDirectoryOnly))
        {

            ModelCar item = new ModelCar().DeserializeFile(File);

            Car.Add(item.Id, item);

        }

    }

问题是编写字典'Car',它将所有汽车数据保存到组合的XML输出/输入中。关于模型'Cars'应该如何以及如何调用的任何提示/教程/示例?

修改:现在查看此帖子 - https://stackoverflow.com/a/19600156/8333405

2 个答案:

答案 0 :(得分:0)

First, you need to fix your XML to have proper end tags, like this:

<?xml version="1.0" encoding="utf-8" ?>
<cars>
  <car id="0">
    <Type>1</Type>
    <Class>Buick</Class>
  </car>
  <car id="1">
    <Type>1</Type>
    <Class>B</Class>
  </car>
</cars>

Then you can let Visual Studio creates your model for you.

  1. Copy you XML file contents.

  2. From Visual Studio Menus select Edit -> Paste Special -> Paste XML as Classes.

Now, Visual Studio generated your models. To use them use this code:

// you do not need a dictionary. A List will do it.
List<carsCar> allCars = new List<carsCar>();

string folder = @"G:\Projects\StackOverFlow\WpfApp2";

foreach (var file in Directory.GetFiles(folder, "*.xml", SearchOption.TopDirectoryOnly))
{
    FileStream reader = File.OpenRead(file);
    XmlSerializer ser = new XmlSerializer(typeof(cars));
    cars cars = (cars)ser.Deserialize(reader);

    allCars.AddRange(cars.car);
}

// if you want the car with id = 0
var car_0 = allCars.FirstOrDefault(c => c.id == 0);

答案 1 :(得分:0)

尝试使用XML Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            Cars cars = new Cars();
            XElement xCars = cars.Serialize();
        }
    }
    public class Cars
    {
        public Dictionary<int, ModelCar> Car { get; set; }


        public Cars()
        {

            Car = new Dictionary<int, ModelCar>() {
                {0, new ModelCar() { _class = "B", _type = 1, id = 0}},
                {1, new ModelCar() { _class = "B", _type = 1, id = 1}}
            };
        }
        public XElement Serialize()
        {
            return new XElement("cars", Car.AsEnumerable().Select(x => new XElement("car", new object[] {
                new XAttribute("id", x.Key),
                new XElement("Type", x.Value._type),
                new XElement("Class", x.Value._class)
            })));
        }
    }
    public class ModelCar
    {
        public int id { get; set; }
        public int _type { get; set; }
        public string _class { get;set; }
    }
}
相关问题