C# net中的XML序列化
我正在尝试序列化一个对象以满足另一个系统需求 它需要如下所示:C# net中的XML序列化,c#,.net,xml,xmlserializer,C#,.net,Xml,Xmlserializer,我正在尝试序列化一个对象以满足另一个系统需求 它需要如下所示: <custom-attribute name="Colour" dt:dt="string">blue</custom-attribute> <custom-attribute>blue</custom-attribute> 我不确定实现这一点需要什么元数据。您可以实现: 基于我需要做的大量定制xml,这太难实现了。所以我创建了一个更简单的黑客解决方案。这似乎是正确的方法。谢谢
<custom-attribute name="Colour" dt:dt="string">blue</custom-attribute>
<custom-attribute>blue</custom-attribute>
我不确定实现这一点需要什么元数据。您可以实现:
基于我需要做的大量定制xml,这太难实现了。所以我创建了一个更简单的黑客解决方案。这似乎是正确的方法。谢谢
[XmlElement("custom-attribute")]
public String Colour{ get; set; }
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);
}
}