【问题标题】:xml serialization / deserialization using models使用模型的xml序列化/反序列化
【发布时间】:2017-12-17 08:01:57
【问题描述】:

我想要实现的是这样的 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);

        }

    }

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

编辑:现在查看这篇文章 - https://stackoverflow.com/a/19600156/8333405

【问题讨论】:

标签: c# xml serialization deserialization models


【解决方案1】:

首先,您需要修复您的 XML 以具有正确的结束标记,如下所示:

<?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>

然后您可以让 Visual Studio 为您创建模型。

  1. 复制您的 XML 文件内容。

  2. 从 Visual Studio 菜单中选择 编辑 -> 选择性粘贴 -> 将 XML 粘贴为类

现在,Visual Studio 生成了您的模型。要使用它们,请使用以下代码:

// 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);

【讨论】:

    【解决方案2】:

    尝试使用 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; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-12
      • 2019-02-24
      • 2011-06-28
      • 1970-01-01
      • 2011-05-03
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多