I'm building a .NET WebApi project. One of my ApiControllers returns a datatable. In JSON format, it all looks good, but the XML-format contains so much junk I don't need.
So, I was thinking, let's write my own XML-serialization. For doing this I've made a new class that implements IXmlSerializable. It looks like this:
public class MyDataTable : IXmlSerializable
{
public MyDataTable(DataTable datatable)
{
this.Data = datatable;
}
public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("Test");
writer.WriteElementString("T", "hello world");
writer.WriteEndElement();
}
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
public DataTable Data { get; set; }
}
Now my XML looks great, but my JSON isn't. The JSON looks like:
{"Data":[{"id":1,"name":"John"},{"id":2,"name":"Julia"}]}
What I really want is this:
[{"id":1,"name":"John"},{"id":2,"name":"Julia"}]
Is there an easy way to remove the "Data"-string from the JSON-result, without rewriting the whole thing? Or is there a better solution than this one?