Jon
Jon

Reputation: 40032

DeSerialize DataTable Row Collection into List<T>

I have a DataTable with rows of data. I have a class with properties that match the row column names.

How can I have a List that is populated from the DataTable Row information?

Do I call something like (MyType)new XmlSerializer(typeof(MyType)).Deserialize(new XMLReader(Table.WriteXML()));

Upvotes: 1

Views: 2284

Answers (2)

Jeff Sternal
Jeff Sternal

Reputation: 48583

I recommend writing classes to perform this transformation instead of using XML serialization, which requires a lot of extra work and ties your objects too closely to your data model.

On the other hand, sometimes you just have to deserialize collections from XML. To accomplish this, all you need to do is tell the XmlSerializer which node to map to the collection.

By default, DataTable.WriteXml creates a root element called <DocumentElement>. For example, if you write from a DataTable called "Name" that has "FirstName" and "LastName" columns, you'll get this:

<DocumentElement>
    <Name>
        <FirstName>Jon</FirstName>
        <LastName>User</LastName>
    </Name>
</DocumentElement>

The problem is that the XmlSerializer doesn't know that "DocumentElement" should be deserialized to your collection class. There are two ways to tell it how.

By Convention

The XmlSerializer knows that a root element named "ArrayOfMyClass" should map to collections of MyClass.

Add your DataTable to a DataSet named "ArrayOfMyClass" to serialize it like this ...

<ArrayOfMyClass>
    <MyClass>
    // ... elements that map to properties of MyClass
    </MyClass>
</ArrayOfMyClass>

.... which deserializes into a List<MyClass> as desired.

By Hand

As an alternative, you can do it like this:

XmlRootAttribute root       = new XmlRootAttribute("DocumentElement");
XmlSerializer    serializer = new XmlSerializer(typeof(List<Name>), root);

Presuming everything else is ok (that is, your data row column names match your class' property names), this will deserialize as expected into your List<MyClass>.

Edit: note that this approach has a rather severe problem (with a moderately cumbersome workaround) described in this SO question: XmlSerializer Performance Issue.

Upvotes: 2

d91-jal
d91-jal

Reputation: 1017

If I understand your question correctly, you want to put the fields of each row of data into instances of YourClass and then store the instances in a List?

In that case, the most straightforward way is to

create the List object
loop over the rows
   create a new YourClass object
   map the fields to the properties of the YourClass object
   add the YourClass object to the list

Upvotes: 1

Related Questions