Reputation: 1793
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.
Upvotes: 2
Views: 5390
Reputation: 19217
Probably you not in right direction using DataTable
to map in and out to your Customer
class. You are in a need of an ORM (Object relation mapper) now. There are certainly quite a few ORMs available for .NET community.
I am quite a big fan of Fluent NHibernate
and even Entity Framework
. These always maps your database table rows to your list of objects.
Probably these are known to you and if you are considering performance and using native data access for .NET. I would then highly recommend you to go for Dapper
and StackOverflow itself uses this ORM. I myself use Dapper when performance becomes an issue and need the comfort of development and clean looking codes.
For your instance, using Dapper would have a class Customer
public class Customer {
public Guid ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Gender { get; set; }
public DateTime RegistrationDate { get; set; }
}
And your code to retrieve data from database to your customer object would be:
IEnumerable<Customer> allCustomers = _yourDapperConnection.Query<Customer>();
That's it! Look at the project's homepage for more details.
Upvotes: 1
Reputation: 63956
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
Upvotes: 5