C# 强制使用特定的xml对象序列化格式
我正在我的一个对象上使用C#序列化:C# 强制使用特定的xml对象序列化格式,c#,xml,C#,Xml,我正在我的一个对象上使用C#序列化: StringWriter outStream = new StringWriter(); XmlSerializer s = new XmlSerializer(obj.GetType()); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); s.Serialize(outStream, obj, ns); string xml = outStream.ToS
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;
}
MyPoints
类正被另一个程序使用,该程序的格式为:
<Points Type="Credit">123</Points>
123
我正在努力工作,如果我可以使用任何属性来强制这种格式
谢谢我认为您需要在数字字段上使用
[System.Xml.Serialization.XmlText]
属性,就像您在类型上使用xmltribute
一样:
public class Points
{
[System.Xml.Serialization.XmlAttribute]
public string Type;
[System.Xml.Serialization.XmlText]
public double Number;
}
请参阅此线程: