我正在尝试序列化一个对象,以满足另一个系统需求。
它需要看起来像这样:
<custom-attribute name="Colour" dt:dt="string">blue</custom-attribute>而是看起来像这样:
<custom-attribute>blue</custom-attribute> 到目前为止,我有这样的想法:
[XmlElement("custom-attribute")]
public String Colour{ get; set; }我真的不确定我需要什么元数据来实现这一点。
发布于 2011-05-19 15:38:31
您可以实现IXmlSerializable
public class Root
{
[XmlElement("custom-attribute")]
public Colour Colour { get; set; }
}
public class Colour : IXmlSerializable
{
[XmlText]
public string Value { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("dt:dt", "", "string");
writer.WriteAttributeString("name", "Colour");
writer.WriteString(Value);
}
}
class Program
{
static void Main()
{
var serializer = new XmlSerializer(typeof(Root));
var root = new Root
{
Colour = new Colour
{
Value = "blue"
}
};
serializer.Serialize(Console.Out, root);
}
}https://stackoverflow.com/questions/6055140
复制相似问题