【问题标题】:Create an XML from a DataTable从 DataTable 创建 XML
【发布时间】:2012-10-15 21:25:36
【问题描述】:

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

【问题讨论】:

标签: c# xml linq datatable dataset


【解决方案1】:

DataTables 旨在遍历行,而不是像您拥有的列。没有任何内置功能可以按列主要顺序保留 DataTable。您将使用自定义代码。伪代码类似于:

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

【讨论】:

  • 我必须跳过第一列。如果有人能给我提供代码,那就太好了
  • 好吧,我添加了跳过“名称”列,但您将不得不自己做一些工作。如果您有具体问题,您可以在此处将它们作为新问题发布,但我怀疑您是否会找人为您写完整的内容。
【解决方案2】:

你可以试试这个

http://msdn.microsoft.com/en-us/library/system.data.datatable.writexml.aspx

DataTable youdatatable = GetData();
System.IO.StringWriter writer = new System.IO.StringWriter();
 youdatatable.WriteXml(writer, XmlWriteMode.WriteSchema, true);
 PrintOutput(writer);

【讨论】:

    【解决方案3】:
    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 2017-12-07
      • 2017-03-26
      • 2012-04-05
      • 2019-04-01
      • 2013-09-16
      相关资源
      最近更新 更多