2017-02-20 71 views
-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 

+0

您可能想看看[XMLAttribute](https:// msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute(v=vs.110).aspx)。 –

+0

當使用XmlAttribute時,我得到關於在原始類型上使用它們的錯誤 – tweetypi

+0

您需要刪除參數'System.Xml.Serialization.XmlAttribute',您還需要定義和刪除模式細節。 –

回答

2

你的原班應該是這樣的:

[System.Serializable] 
[System.Xml.Serialization.XmlRoot("Color")] 
public class UIColor 
{ 
    public UIColor() 
    { 
     name = "Col1" 
    } 

    public UIColor(double red, double green, double blue, double alpha) 
    { 
     r = (float)red; 
     g = (float)green; 
     b = (float)blue; 
     a = (float)alpha; 
     name = "Col1"; 
    } 

    public UIColor(double white, double alpha) 
    { 
     r = (float)white; 
     g = (float)white; 
     b = (float)white; 
     a = (float)alpha; 
     name = "Col1"; 
    } 

    [System.Xml.Serialization.XmlAttribute] 
    public string name 
    { 
     get; 
     set; 
    } 


    [System.Xml.Serialization.XmlAttribute] 
    public float r 
    { 
     get; 
     set; 
    } 

    [System.Xml.Serialization.XmlAttribute] 
    public float g 
    { 
     get; 
     set; 
    } 

    [System.Xml.Serialization.XmlAttribute] 
    public float b 
    { 
     get; 
     set; 
    } 

    [System.Xml.Serialization.XmlAttribute("alpha")] 
    public float a 
    { 
     get; 
     set; 
    } 
} 

並且序列代碼:

using (System.IO.TextWriter writer = new System.IO.StreamWriter(@"C:\temp\test.xml")) 
{ 
     System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(UIColor)); 
     System.Xml.Serialization.XmlSerializerNamespaces namspace = new XmlSerializerNamespaces(); 
     namespace.Add("", ""); 
     xml.Serialize(writer, new UIColor(), namespace); 
} 

和OUT XML會產生:

<?xml version="1.0" encoding="utf-8"?> 
<Color name="Col1" r="0" g="0" b="0" alpha="0" />