Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9232949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:26:06+00:00 2026-06-18T06:26:06+00:00

I’m building a .NET WebApi project. One of my ApiControllers returns a datatable. In

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:26:07+00:00Added an answer on June 18, 2026 at 6:26 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am confused How to use looping for Json response Array in another Array.
I am using JSon response to parse title,date content and thumbnail images and place
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.