gpt4 book ai didi

c# - 从 DataTable 创建 XML

转载 作者:数据小太阳 更新时间:2023-10-29 01:47:13 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com