我正在尝试将一个类对象序列化为以下类似的xml格式:
<Colors>
<Blue>
<R>0,000</R>
<G>0,000</G>
<B>1,000</B>
<A>1,000</A>
</Blue>
<Red>
<R>1,000</R>
<G>0,000</G>
<B>0,000</B>
<A>1,000</A>
</Red></Colors>
重要的一点是蓝色和红色没有直接指定。我有一个这样的类:
public class Color
{
[XmlElement("R")]
public string red;
[XmlElement("G")]
public string green;
[XmlElement("B")]
public string blue;
[XmlElement("A")]
public string alpha;
}
我需要的是一种创建Color类对象实例并将其序列化为不同名称(如blue、red、green、anothercolor1、anothercolor2等)的方法,同时在程序运行时动态添加新颜色也必须可行。 我知道我可以向Color类添加属性,但无法更改XML的布局,因此必须找到另一种方法。 有什么想法吗?
<Colors><Color Name="Red">..</Color></Colors>
。 - Bas