【发布时间】:2011-03-05 10:07:59
【问题描述】:
我需要将 CSV 转换为 XML 文档。到目前为止,我看到的示例都显示了如何在 CSV 中使用固定数量的列来执行此操作。
到目前为止,我使用 LINQ:
String[] File = File.ReadAllLines(@"C:\text.csv");
String xml = "";
XElement top = new XElement("TopElement",
from items in File
let fields = items.Split(';')
select new XElement("Item",
new XElement("Column1", fields[0]),
new XElement("Column2", fields[1]),
new XElement("Column3", fields[2]),
new XElement("Column4", fields[3]),
new XElement("Column5", fields[4])
)
);
File.WriteAllText(@"C:\xmlout.xml", xml + top.ToString());
这是针对固定数量的列,但我的 .CSV 在每行有不同数量的列。
根据 .CSV 的每一行中有多少单词(列),您如何将某种循环放入其中?
谢谢
【问题讨论】: