从DataTable创建一个XML [英] Create an XML from a DataTable

查看:192
本文介绍了从DataTable创建一个XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

 < ListDataCollat​​eralDials> 
< DataCollat​​eralDials Type =Conv>
<乘数> 1< /乘数>
<调味品> 1< /调味料>
< Lockin> 1< / Lockin>
<乘数> 1< /乘数>
< ElbowShift> 0< / ElbowShift>
< Steepness> 1< / Steepness>
< Burnout> 1< / Burnout>
<调整> 1< / Adjustment>
<效果> 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>

< / DataCollat​​eralDials>
< DataCollat​​eralDials Type =Conv>
<乘数> 1< /乘数>
<调味品> 1< /调味料>
< Lockin> 1< / Lockin>
<乘数> 1< /乘数>
< ElbowShift> 0< / ElbowShift>
< Steepness> 1< / Steepness>
< Burnout> 1< / Burnout>
<调整> 1< / Adjustment>
<效果> 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>

< / DataCollat​​eralDials>
< / ListDataCollat​​eralDials>


解决方案

  public static string ToXml(此DataTable表,int metaIndex = 0)
{
XDocument xdoc = new XDocument(
new XElement(table.TableName,
from table.Calumns.Cast< ; DataColumn>()
其中column!= table.Columns [metaIndex]
选择新的XElement(column.ColumnName,
from table.AsEnumerable()
选择新的XElement (row.Field< string>(metaIndex),row [column])


);

return xdoc.ToString();
}

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


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.

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

This worked great for me. Thanks stackoverflow.

这篇关于从DataTable创建一个XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆