出口SQL Server数据库表到XML使用LINQ

问题描述:

我知道该怎么做简单的LINQ到XML。导出SQL Server数据库表中的数据。像遵循一个简单的查询将工作:

I know how to do simple Linq To XML. Export a sql server database table data. A simple query like follow will work:

xmlDoc = new XElement("TestPoints",
                from test in myDB.TestPoints
                select
                new XElement("TestPoint",
                    new XElement("Id", test.Id),
                    new XElement("Value", test.Value),
                    new XElement("Time", test.Time),
                    new XElement("TestId", test.TestId)
                    )
                );
            xmlDoc.Save("test.xml");

然而,在这种情况下,我需要指定每个数据库列那些我需要出口。我需要的是要导出的有30多个列的表。现在,它的有点痛苦,使创建新的XElement的这种重复任务。 有什么办法来转储全部表及其所有列/行到一个XML文件在LINQ容易吗?如果没有外部指定每一列即可。谢谢你。

However, in this case, i need to specify every database column those I need to export. What I need is to export a table which have more than 30 columns. Now, its a bit painful to make such repetitively task of creating new XElement. Is there any way to dump the full table and all its columns/rows to a xml file in LinQ easily please? Without specify each column externally. Thanks.

在数据表中放置数据的SQL Server表

Put Data of sql server table in DataTable

比使用 DataTable.WriteXml 会做你的任务很容易。

and than use DataTable.WriteXml will do your task easily.