How to export a DataTable to Xml with ALL columns as attributes?

前端 未结 5 1661
执笔经年
执笔经年 2020-12-31 18:58

Question: I\'m exporting a System.Data.DataTable to XML. So far it works fine. But I want to have all the data in attributes, which works fine as well. But my problem now, i

5条回答
  •  无人及你
    2020-12-31 19:31

    Two points:

    First: The ExportTable() threw an exception: "DataTable already belongs to another DataSet." When I executed:

    ds.Tables.Add(dtt)
    

    I corrected this by making a local copy of the table:

    Dim dtX As DataTable = dtt.Copy
    ds.Tables.Add(dtX)
    ds.DataSetName = strTable
    

    This worked well.

    Second: If you use the XML to create a dynamic SQL statement, there is no need to be concerned about the columns/fields with NULL value being omitted in the XML export. Simply walk through the a attributes that are in the XML record, build the INSERT or UPDATE statement and execute a connection command. This is faster than using a DataSet.

    For INSERT it has one drawback. If the primary key is created by incrementing an identity column, an ADO.Net DataSet will return it. Dynamic SQL will require a SELECT statement to retrieve it.

    Also, it would be good idea to obfuscate your code.

提交回复
热议问题