简体   繁体   中英

XML Serialization in C# for nested nodes

I want to create an XML document in the following structure:

<Fruits>
  <Fruit>
   <FruitName>Apple</FruitName>
      <Color>
    <Color1>Green</Color1>  
        <Color2>Green</Color2>
      </Color>
  </Fruit>
  <Fruit>
   <FruitName>Lemon</FruitName>
      <Color>
    <Color1>Green</Color1>  
        <Color2>Yellow</Color2>
      </Color>
  </Fruit>
<Fruit>
   <FruitName>Orange</FruitName>
      <Color Value="Orange">
      </Color>
  </Fruit>
</Fruits>

I have a class:

    [Serializable()]
    public class Fruit
    {
        [XmlElement(ElementName = "FruitName", Order = 1)]
        public string "FruitName", { get; set; }

        [XmlElement(ElementName = "Color", Order = 2)]
        public Color c =new Color();

        public Fruit(string fruitname,  Dictionary<string, string> colorDictionary)
        {
//constructor to set values for fruitname and dictionary as received from the calling class
            fruitName = fruitname;
            foreach (KeyValuePair<string, string> entry in colorDictionary)
            {
                 c = new Color(entry.Key, entry.Value);
            }
        }
    }
    public class Color
    {
        [XmlElement(ElementName = "Color1", IsNullable = true)]
        public string Color1 { get; set; }

        [XmlElement(ElementName = "Color2", IsNullable = true)]
        public string Color2 { get; set; }

        [XmlAttribute("Value")]
        public string Value { get; set; }
    /// <summary>
    /// Parameterless constructor for serialization.
    /// </summary>
    public Color() { }

    /// <summary>
    /// Parameterized constructor for getting and setting values.
    /// </summary>
    public Color(string col1, string Col2)
    {
        Color1 = col1;
        Color2 = col2;
    }
}

I don't understand but there's some issue in the code but I am unable to find what since I am not able to serialize. I'm getting the error:

System.InvalidOperationException: There was an error reflecting type 'System.Collections.Generic.List`1

Fruit f = new Fruit(fruitName, colorDictionary); 
Fruits.Add(fruit);
XmlSerializer serializer = new XmlSerializer(typeof(List<Fruit>), new XmlRootAttribute("Fruits"));

I suppose that Fruit should also have parameterless constructor. Consider the following example:

public static void Main(string[] args)
{
    string appleName = "Apple";
    Dictionary<string, string> appleColors = new Dictionary<string, string>
    {
        { "Green", "Green" }
    };
    string lemonName = "Lemon";
    Dictionary<string, string> lemonColors = new Dictionary<string, string>
    {
        { "Green", "Yellow" }
    };
    string orangeName = "Orange";
    Dictionary<string, string> orangeColors = new Dictionary<string, string>
    {
        { "Orange", "Orange" }
    };

    var fruits = new List<Fruit>();
    Fruit apple = new Fruit(appleName, appleColors);
    Fruit lemon = new Fruit(lemonName, lemonColors);
    Fruit orange = new Fruit(orangeName, orangeColors);
    fruits.Add(apple);
    fruits.Add(lemon);
    fruits.Add(orange);

    XmlSerializer serializer = new XmlSerializer(typeof(List<Fruit>), new XmlRootAttribute("Fruits"));
    using (var stream = new FileStream("fruits.xml", FileMode.CreateNew))
    {
        using(var wr = new StreamWriter(stream))
        {
            serializer.Serialize(wr, fruits);
        }
    }

    Console.ReadKey();
}

[Serializable()]
public class Fruit
{
    [XmlElement(ElementName = "FruitName", Order = 1)]
    public string FruitName { get; set; }

    [XmlElement(ElementName = "Color", Order = 2)]
    public Color c = new Color();

    public Fruit()
    {

    }

    public Fruit(string fruitname, Dictionary<string, string> colorDictionary)
    {
        FruitName = fruitname;
        foreach (KeyValuePair<string, string> entry in colorDictionary)
        {
            c = new Color(entry.Key, entry.Value);
        }
    }
}

public class Color
{
    [XmlElement(ElementName = "Color1", IsNullable = true)]
    public string Color1 { get; set; }

    [XmlElement(ElementName = "Color2", IsNullable = true)]
    public string Color2 { get; set; }

    [XmlAttribute("Value")]
    public string Value { get; set; }

    /// <summary>
    /// Parameterless constructor for serialization.
    /// </summary>
    public Color() { }

    /// <summary>
    /// Parameterized constructor for getting and setting values.
    /// </summary>
    /// <param name="torque"></param>
    public Color(string col1, string col2)
    {
        Color1 = col1;
        Color2 = col2;
    }
}

Hope it helps.

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