2017-02-20 7 views
-3

私はそうのようなクラスを持っている:私がやりたい何C#で特定の形式でXMLをシリアル化しますか?

class Colors 
{ 
[XmlElement("Col1")] 
    UIColor Col1; 
[XmlElement("Col2")] 
    UIColor Col2; 
    //etc etc 
} 

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

そして、それの多くのインスタンス次の形式:

<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" /> 
関連する問題