2012-02-09 27 views
0

我用我的對象之一的C#序列化:強制特定的XML對象序列化格式

StringWriter outStream = new StringWriter(); 
    XmlSerializer s = new XmlSerializer(obj.GetType()); 
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
    s.Serialize(outStream, obj, ns); 
    string xml = outStream.ToString(); 

目的是:

public class Points 
{ 
    [System.Xml.Serialization.XmlAttribute] 
    public string Type; 
    public double Number; 
} 

Points類是被另一個程序期待它的形式:

<Points Type="Credit">123</Points> 

我試圖工作,如果我可以使用任何屬性強制這種格式?

感謝

+1

看到這個線程:http://stackoverflow.com /問題/ 307433 /如何對序列化到日期時間 – twilson 2012-02-09 10:01:37

回答

1

我認爲你需要使用[System.Xml.Serialization.XmlText]屬性上的數字領域,比如你用XmlAttribute的類型做:

public class Points 
{ 
    [System.Xml.Serialization.XmlAttribute] 
    public string Type; 
    [System.Xml.Serialization.XmlText] 
    public double Number; 
}