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 6822579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:40:49+00:00 2026-05-26T21:40:49+00:00

A few days back I had to de-serialize data from xml file to my

  • 0

A few days back I had to de-serialize data from xml file to my List class.
my approach was like this:

private void button1_Click(object sender, EventArgs e)
{
    XmlDocument doc = new XmlDocument();
    doc.Load("Test.xml");
    XmlSerializer xs = new XmlSerializer(typeof(UserHolder));
    UserHolder uh = (UserHolder)xs.Deserialize(new StringReader(doc.OuterXml));
    dataGridView1.DataSource = uh.Users.ToDataTable();
}
public class User
{
    [XmlElement("id")]
    public Int32 Id { get; set; }

    [XmlElement("name")]
    public String Name { get; set; }
}

[XmlRoot("user_list")]
public class UserHolder
{
    private List<User> users = null;

    public UserHolder()
    {
        users = new List<User>(); 
    }

    [XmlElement("user")]
    public List<User> Users
    {
        get { return users; }
        set { users = value; }
    }
}

public static class Convert2Table
{
    public static DataTable ToDataTable<T>(this IList<T> list)
    {
        PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        for (int i = 0; i < props.Count; i++)
        {
            PropertyDescriptor prop = props[i];
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        }
        object[] values = new object[props.Count];
        foreach (T item in list)
        {
            for (int i = 0; i < values.Length; i++)
                values[i] = props[i].GetValue(item) ?? DBNull.Value;
            table.Rows.Add(values);
        }
        return table;
    }
}

The code above works fine, but I had to write many lines of code.

I now have a new situation where I have a customer class and one datatable. My datatable is populated with multiple customers data. I want to deserialize that data from datatable to my customer class. How can I do that more efficiently than I did before? I doubt linq will help me. So please guide me for deserialize datatable data to my customer class List.

  • 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-05-26T21:40:50+00:00Added an answer on May 26, 2026 at 9:40 pm

    Like this:

    DataTable dt .... 
    
    
    List<Customer>  customers = (from c in dt.AsEnumerable()
                    select new Customer { ID = c.Field<int>("CustomerID"), 
                    Name=c.Field<string>("CustomerName")  }).ToList();
    

    And so on for all the properties of Customer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I bought a VPS a few days back and had a domain name registered.
A few days back I felt this question to be dumb and dint post
I started reading about implementing various data structures a few days back, and got
I friend of mine and I had a discussion a few days back about
This is a follow-up question to an answer I gave a few days back
Few days back, I posted a question about getting student marks results from database
I run XAMPP, a few days back i had set up a password for
Few days back i upgrade to 10.6 and have to install everything from start.
A few days back, while writing an answer for this question here on overflow
A few days back I had an interview, and I was asked to write

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.