Create an XML from a DataTable(从 DataTable 创建 XML)
问题描述
使用 C#:我想将此表转换为 XML.请忽略行名中的错误.这是测试数据.我已经给出了两列转换为 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.
<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.
This worked great for me. Thanks stackoverflow.
这篇关于从 DataTable 创建 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 DataTable 创建 XML
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01