Export SQL Server Database Table to XML Using Linq

别等时光非礼了梦想. 提交于 2019-12-10 10:21:27

问题


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");

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.


回答1:


Put Data of sql server table in DataTable

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



来源:https://stackoverflow.com/questions/6453865/export-sql-server-database-table-to-xml-using-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!