Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET WebApi + DataTable

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?

like image 708
JeanD Avatar asked Jan 28 '13 21:01

JeanD


1 Answers

I have written my own datatable serialization to achieve my goal.

For the interested folk:

[JsonConverter(typeof(DataTableConverter))]
[XmlRoot("Result")]
public class MyDataTable : IXmlSerializable
{
    public MyDataTable(DataTable datatable)
    {
        this.Data = datatable;
    }


    public void WriteXml(XmlWriter writer)
    {
        foreach (DataRow row in Data.Rows)
        {
            writer.WriteStartElement(Data.TableName);
            foreach (DataColumn column in row.Table.Columns)
            {
                writer.WriteElementString(column.ColumnName, row[column].ToString());
            }
            writer.WriteEndElement();
        }
    }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        throw new NotImplementedException();
    }
    public DataTable Data { get; set; }
}

And a custom JSON converter:

public class DataTableConverter : JsonConverter
{

    public override bool CanConvert(Type objectType)
    {
        return typeof(MyDataTable).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        MyDataTable myDataTable = value as MyDataTable;
        DataTable dt = myDataTable.Data;

        writer.WriteStartArray();

        foreach (DataRow row in dt.Rows)
        {
            writer.WriteStartObject();
            foreach (DataColumn column in row.Table.Columns)
            {
                writer.WritePropertyName(column.ColumnName);
                serializer.Serialize(writer, row[column]);
            }
            writer.WriteEndObject();
        }

        writer.WriteEndArray();
    }
}

Implement the new DataTable class like this:

public class ValuesController : ApiController
{
    // GET api/values
    public MyDataTable Get()
    {
        DataTable dt = new DataTable();
        dt.TableName = "info";
        using (SqlConnection cn = new SqlConnection("Data Source=(local);Initial Catalog=ApiPoc;Integrated Security=true;"))
        {
            cn.Open();
            using (SqlCommand cm = new SqlCommand("select * from employee",cn))
            {
                SqlDataReader dr = cm.ExecuteReader();
                dt.Load(dr);
            }
        }
        MyDataTable md = new MyDataTable(dt);
        return md;
    }
}
like image 179
JeanD Avatar answered Jan 03 '23 00:01

JeanD