从DataTable创建XML

7

以下是C#中的DataTable格式

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

 <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>

在第二个xelement中,<DataCollateralDials Type="glen">这是一个打字错误。 - StackOverflowVeryHelpful
使用 DataTable.WriteXml http://msdn.microsoft.com/en-us/library/system.data.datatable.writexml.aspx - Prabhu Murthy
3个回答

5
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。


3

DataTable是为了迭代行而设计的,而不是像你所拥有的那样迭代列。没有内置的方式可以将DataTable按列顺序持久化。你需要使用自定义代码。伪代码可能如下:

foeeach(DataColumn)
  if(name != "Name")
    output column header
    foreach(DataRow) 
      output row value

我必须跳过第一列。如果有人能为我提供代码,那就太好了。 - StackOverflowVeryHelpful
我已经跳过了“Name”列,但你仍需要自己做一些工作。如果你有具体问题可以在这里发布新的问题,但我怀疑你不会得到别人替你写整个内容的机会。 - D Stanley

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接