2012-02-09 54 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; 
}