【发布时间】:2014-05-29 08:18:43
【问题描述】:
我正在尝试从 ASP.NET 中的 CSV 文件构建 XML 字符串。我不需要显示 XML 页面,因为我正在构建 XML 字符串然后发送到 API。 CSV 托管在服务器上。
我需要能够循环遍历 CSV,直到出现空白行(行会随着我使用 CSV 而有所不同),然后停止循环。它会是这样的(<Info> 将是 CSV 中不同行的内容):
<Example>
<Info>
<Name>This would be the name</Name>
<Address>This would be the address</Address>
<Email>This would be the email</Email>
</Info>
<Info>
<Name>This would be the name</Name>
<Address>This would be the address</Address>
<Email>This would be the email</Email>
</Info>
<Info>
<Name>This would be the name</Name>
<Address>This would be the address</Address>
<Email>This would be the email</Email>
</Info>
</Example>
这是一个示例,说明如果 CSV 中有三行,它将如何工作。
我在 Stack Overflow 上看到了这段代码:
var lines = File.ReadAllLines("my_csv.csv");
var xml = new XElement("TopElement",
lines.Select(line => new XElement("Item",
line.Split(';')
.Select((column, index) => new XElement("Column" + index, column)))));
xml.Save("my_csv.csv");
我试过了(替换我的 csv 文件名和路径),但该页面无法加载。
关于我做错了什么有什么想法吗?谢谢。
【问题讨论】:
标签: c# asp.net xml csv stringbuilder