简体   繁体   English

反序列化DataTable行集合到列表中<T>

[英]DeSerialize DataTable Row Collection into List<T>

I have a DataTable with rows of data. 我有一个包含数据行的DataTable。 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? 如何获得从DataTable行信息填充的列表?

Do I call something like (MyType)new XmlSerializer(typeof(MyType)).Deserialize(new XMLReader(Table.WriteXML())); 我调用类似(MyType)new XmlSerializer(typeof(MyType))。Deserialize(new XMLReader(Table.WriteXML()))的东西吗?

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. 我建议编写类来执行此转换,而不要使用XML序列化,这需要大量额外的工作,并且会使对象与数据模型之间的联系过于紧密。

On the other hand, sometimes you just have to deserialize collections from XML. 另一方面,有时您只需要反序列化XML的集合。 To accomplish this, all you need to do is tell the XmlSerializer which node to map to the collection . 为此, 您需要做的就是告诉 XmlSerializer 哪个节点映射到collection

By default, DataTable.WriteXml creates a root element called <DocumentElement> . 默认情况下, DataTable.WriteXml创建一个名为<DocumentElement>的根元素。 For example, if you write from a DataTable called "Name" that has "FirstName" and "LastName" columns, you'll get this: 例如,如果您从具有“ FirstName”和“ LastName”列的名为“ Name”的DataTable写入DataTable ,则会得到以下信息:

<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. 问题在于XmlSerializer不知道应该将“ DocumentElement”反序列化到您的集合类中。 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 . XmlSerializer知道一个名为“ ArrayOfMyClass”的根元素应映射到MyClass集合。

Add your DataTable to a DataSet named "ArrayOfMyClass" to serialize it like this ... 您添加DataTableDataSet名为“ArrayOfMyClass”连载像这样...

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

.... which deserializes into a List<MyClass> as desired. ....根据需要反序列化为List<MyClass>

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> . 假设其他一切都正常(也就是说,数据行列名称与类的属性名称匹配),这将按预期反序列化到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 . 编辑:请注意,此方法存在一个非常严重的问题(有一个相对繁琐的解决方法),此SO问题: XmlSerializer性能问题

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? 如果我正确理解了您的问题,您想将每行数据的字段放入YourClass实例中,然后将这些实例存储在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM