C#中针对嵌套节点的XML序列化

时间:2018-09-06 14:39:10

标签: c# xml serialization

我想以以下结构创建XML文档:

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

我有一堂课

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

我不明白,但是代码中存在一些问题,但是由于无法序列化,所以我找不到任何内容。我遇到了错误:

  

System.InvalidOperationException:出现错误,反映了类型'System.Collections.Generic.List`1

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

1 个答案:

答案 0 :(得分:0)

我想Fruit也应该有无参数的构造函数。考虑以下示例:

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

希望有帮助。