c# - 从 DataTable 创建 XML

标签 c# xml linq datatable dataset

DataTable In C# of following format

使用 C#:我想将此表转换为 XML。 请忽略行名中的错误。这是测试数据。我已经给出了转换为 xml 的两列示例和相应的行作为属性。但我实际上想要所有专栏。这是一个数据表。

 <ListDataCollateralDials>
                                <DataCollateralDials Type="Conv">
                                    <Multiplier>1</Multiplier>
                                    <Seasoning>1</Seasoning>
                                    <Lockin>1</Lockin>
                                    <Multiplier>1</Multiplier>
                                    <ElbowShift>0</ElbowShift>
                                    <Steepness>1</Steepness>
                                    <Burnout>1</Burnout>
                                    <Adjustment >1</Adjustment>
                                    <Effect>1</Effect>
                                    <Decay>1</Decay>
                                    <Outs>1</Outs>
                                    <Base>700</Base>
                                    <Slope>1</Slope>
                                    <Base>80</Base>
                                    <Slope2>1</Slope2>
                                    <Base2>200</Base2>
                                    <Slope3>1</Slope3>
                                    <Height>0</Height>
                                    <Length>0</Length>
                                    <Height2>0</Height2>
                                    <Length2>0</Length2>
                                    <Elbow>0</Elbow>
                                                 <Multiplier2>1</Multiplier2>
                                    <Multiplier3>1</Multiplier3>

                                </DataCollateralDials>
<DataCollateralDials Type="Conv">
                                <Multiplier>1</Multiplier>
                                <Seasoning>1</Seasoning>
                                <Lockin>1</Lockin>
                                <Multiplier>1</Multiplier>
                                <ElbowShift>0</ElbowShift>
                                <Steepness>1</Steepness>
                                <Burnout>1</Burnout>
                                <Adjustment >1</Adjustment>
                                <Effect>1</Effect>
                                <Decay>1</Decay>
                                <Outs>1</Outs>
                                <Base>700</Base>
                                <Slope>1</Slope>
                                <Base>80</Base>
                                <Slope2>1</Slope2>
                                <Base2>200</Base2>
                                <Slope3>1</Slope3>
                                <Height>0</Height>
                                <Length>0</Length>
                                <Height2>0</Height2>
                                <Length2>0</Length2>
                                <Elbow>0</Elbow>
                                <Multiplier2>1</Multiplier2>
                                <Multiplier3>1</Multiplier3>

                            </DataCollateralDials>
</ListDataCollateralDials>

最佳答案

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

    return xdoc.ToString();
}

这对我来说效果很好。感谢 stackoverflow。

关于c# - 从 DataTable 创建 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13072927/

相关文章:

c# - OWIN ASP.NET - 如果访问 token 已过期,则无法使用刷新 token 生成访问 token

c# - TryParse 无法正确解析

mysql - Perl XML::Smart 内存不足!错误

c#:如何从 linq 查询中设置组合框值成员

c# - Linq SELECT 与 ExecuteQuery

c# - 从提升进程开始非提升进程

c# - 通过传递主键列表在Entity Framework中使用Find方法的多个记录

java - Java从XML文件中读取数据

xml - Mocha Istanbul 尔覆盖 xml 文件生成

c# - Linq 和 DBNull - 出现错误