Create an XML from a DataTable

后端 未结 3 1593
离开以前
离开以前 2020-12-16 04:30

\"DataTable

Using C# : I want to convert this table into XML. Please ignore

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 05:07

    public static string ToXml(this DataTable table, int metaIndex = 0)
    {
        XDocument xdoc = new XDocument(
            new XElement(table.TableName,
                from column in table.Columns.Cast()
                where column != table.Columns[metaIndex]
                select new XElement(column.ColumnName,
                    from row in table.AsEnumerable()
                    select new XElement(row.Field(metaIndex), row[column])
                    )
                )
            );
    
        return xdoc.ToString();
    }
    

    This worked great for me. Thanks stackoverflow.

提交回复
热议问题