gpt4 book ai didi

c# - C# 中用于嵌套节点的 XML 序列化

转载 作者:太空宇宙 更新时间:2023-11-03 14:50:06 24 4
gpt4 key购买 nike

我想创建一个具有以下结构的 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: 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"));

最佳答案

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

希望对您有所帮助。

关于c# - C# 中用于嵌套节点的 XML 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52206603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com