从数据表创建XML

时间:2021-01-31 20:21:24

从数据表创建XML

Using C# : I want to convert this table into XML. Please ignore the mistakes in row names. This is test data. I have given sample of two columns converted to xml and the corresponding rows as attributes . But i actually want for all columns. This is a Datatable.

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

3 个解决方案

#1


4  

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();
}

This worked great for me. Thanks *.

这对我很有效。谢谢*。

#2


3  

DataTables are designed to iterate over rows, not columns like you have. There's nothing built-in to persist a DataTable in column-major order. You're going to have use custom code. Pseudo-code would be something like:

DataTables被设计为遍历行,而不是像您这样的列。没有任何内置的东西可以以列-主顺序持久化数据表。你需要使用自定义代码。伪代码是这样的:

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

#3


2  

you can try using this

你可以试试这个

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

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

#1


4  

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();
}

This worked great for me. Thanks *.

这对我很有效。谢谢*。

#2


3  

DataTables are designed to iterate over rows, not columns like you have. There's nothing built-in to persist a DataTable in column-major order. You're going to have use custom code. Pseudo-code would be something like:

DataTables被设计为遍历行,而不是像您这样的列。没有任何内置的东西可以以列-主顺序持久化数据表。你需要使用自定义代码。伪代码是这样的:

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

#3


2  

you can try using this

你可以试试这个

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

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