-3
我有一個類,像這樣:在C#中以特定格式序列化xml?
[System.Serializable]
public class UIColor
{
public UIColor()
{
}
public UIColor(double red, double green, double blue, double alpha)
{
r = (float)red;
g = (float)green;
b = (float)blue;
a = (float)alpha;
}
public UIColor(double white, double alpha)
{
r = (float)white;
g = (float)white;
b = (float)white;
a = (float)alpha;
}
[System.Xml.Serialization.XmlElement("r", typeof(float))]
public float r
{
get;
set;
}
[System.Xml.Serialization.XmlElement("g", typeof(float))]
public float g
{
get;
set;
}
[System.Xml.Serialization.XmlElement("b", typeof(float))]
public float b
{
get;
set;
}
[System.Xml.Serialization.XmlElement("alpha", typeof(float))]
public float a
{
get;
set;
}
}
而且在像這樣一類的它的許多實例:
class Colors
{
[XmlElement("Col1")]
UIColor Col1;
[XmlElement("Col2")]
UIColor Col2;
//etc etc
}
我希望做的是序列化出來的類顏色成XML格式如下:
<Color name="Col1" r="1" g="1" b="1" alpha="1"/>
<Color name="Col2" r="2" g="2" b="2" alpha="2"/>
目前的方式,序列化出像:
<Col1>
<r>1</r>
//etc etc
您可能想看看[XMLAttribute](https:// msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute(v=vs.110).aspx)。 –
當使用XmlAttribute時,我得到關於在原始類型上使用它們的錯誤 – tweetypi
您需要刪除參數'System.Xml.Serialization.XmlAttribute',您還需要定義和刪除模式細節。 –