简体   繁体   中英

XML Serializing list of objects containing list of objects

My object structure is similar to the simplified code below. Please note that both Countries and Cars need to be classes, I can't use string list/array due to code not included in sample. I want to XML serialize and later deserialize the objects.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace XMLapp
{
    public partial class Form1 : Form
    {
        List<Countries> Country = new List<Countries>();
        List<string> cars = new List<string>();

        public Form1()
        {
            InitializeComponent();

            cars.Add("Audi");
            cars.Add("BMW");
            cars.Add("Mercedes");
            addCountry("Germany", cars);

            cars.Clear();
            cars.Add("Ford");
            cars.Add("Chevrolet");
            cars.Add("Jeep");
            addCountry("USA", cars);

            TestXmlSerialize();
            Console.WriteLine("Generated list");
        }

        void TestXmlSerialize()
        {
            XmlSerializer x = new XmlSerializer(Country.GetType());
            x.Serialize(Console.Out, Country);
        }

        void addCountry(string name, List<string> cars)
        {
            Countries newCountry = new Countries();
            newCountry.Name = name;
            newCountry.AddCar(cars);
            Country.Add(newCountry);
        }
    }

    public class Countries
    {
        public string Name { get; set; }
        List<Cars> car = new List<Cars>();

        public void AddCar(List<string> cars)
        {
            for (int i = 0; i < cars.Count; i++)
            {
                Cars newCar = new Cars();
                newCar.brand = cars[i];
                car.Add(newCar);
            }
        }

        class Cars
        {
            public string brand;
        }

    }
}

This generates following output:

<?xml version="1.0" encoding="IBM437"?>
<ArrayOfCountries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Countries>
    <Name>Germany</Name>
  </Countries>
  <Countries>
    <Name>USA</Name>
  </Countries>
</ArrayOfCountries>

However, I expected something along the lines of

  <Countries>
    <Name>Germany</Name>
    <ArrayOfCars>
      <Brand>Audi</Brand>
      <Brand>BMW</Brand>
      <Brand>Mercedes</Brand>
    </ArrayOfCountries>
  </Countries>

I can see that the car brands are stored properly in the Locals & Autos window, but how do I include them in the serialization?

XmlSerializer only serializes public fields and properties. You need to make the 'car' field and the class 'Cars' public.

It won't produce the exact xml layout that you posted in your question, but it will let you serialize and deserialize the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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