c#如何将CSV转换为xml
本文关键字:转换 xml CSV | 更新日期: 2023-09-27 18:08:54
我知道将CSV文件转换为XML的基本知识,但我的CSV文件有不同的列,我也需要它以不同的方式拆分它。我如何能够使用c#并将我的CSV文件转换为此XML文件?谢谢你的帮助!
<CARS>
<NAME="amk">
<CAR>
<id>A1</id>
<start>12.00</start>
<end>11.59</end>
<place>PARK</place>
<day>DAY</day>
<letter>abc</letter>
<number>77</number>
</CAR>
<CAR>
<id>A2</id>
<start>01.00</start>
<end>12.59</end>
<place>garden</place>
<day>night</day>
<letter>abc</letter>
<number>27</number>
<length>2.15M</length>
<amount>12</amount>
</CAR>
<CAR>...</CAR>
<CAR>...</CAR>
<NAME="bbk">
<CAR>
<id>B1</id>
<start>23.59</start>
<end>11.59</end>
<place>Rooftop</place>
<day>DAY</day>
<letter>abc</letter>
<number>34</number>
</CAR>
<CAR>
<id>B4</id>
<start>01.00</start>
<end>02.00</end>
<place>garden</place>
<day>Day</day>
<letter>abc</letter>
<number>27</number>
<length>2.00M</length>
<amount>2</amount>
</CAR>
<CAR>...</CAR>
<CAR>...</CAR>
</NAME>
</CARS>
public static void ConvertCsvToXML()
{
String[] FileContent = File.ReadAllLines(@"C:'test.csv");
String XMLNS = "";
XElement Inv = new XElement("CARS",
from AREA in FileContent
let fields = AREA.Split(',')
select new XElement("Area", fields[0]),
from CAR in FileContent
let fields = CAR.Split(',')
select new XElement("Car",
new XElement("id", fields[1]),
new XElement("start", fields[2]),
new XElement("end", fields[3]),
new XElement("place", fields[4]),
new XElement("day", fields[5]),
new XElement("letter", fields[6]),
new XElement("number", fields[7]),
new XElement("length", fields[8]),
new XElement("amount", fields[9])
)
);
File.WriteAllText(@"C:'testCSV.xml", XMLNS + Inv.ToString());
}
需要从CSV导入到XML的数据超过100个,列和行数量不同。请帮助!这是代码,我一直在使用到目前为止,但它不会为我工作,因为我没有一定的行,一些数据,我需要7行,一些我需要9或10。另外,我需要创建一个新类别,比如
我的CSV源代码例如是amk, A1,12.00,11.59,PARK,DAY,abc,77 A1,01.00,12.59,garden,night,abc,27,2.15 m, 12bbk, B1,23.59,11.59,Rooftop,DAY,abc, 34b4,01.00,02.00,garden, DAY,abc, 27,2.00 m,2请告诉我如何我需要改变我的CSV代码,让我想要的XML输出工作。谢谢!
让你开始:
- 使用System.Xml.Linq
- 支持不同的分隔符,
- demo使用标题行作为元素名
- 演示如何添加linenumber
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace TestProgram
{
static class Program
{
// NET4 has this in System.IO.File
private static IEnumerable<string> ReadAllLines(string fname)
{
using (var r = new StreamReader(fname))
{
var line = r.ReadLine();
while (null != line)
{
yield return line;
line = r.ReadLine();
}
}
}
private static string[] CsvFields(string line, char[] delim)
{
return null==line
? null
: line.Split(delim, StringSplitOptions.None);
}
public static IEnumerable<T> ProjectCsv<T>(this IEnumerable<string> lines, char[] delim, Func<string[], T> projection)
{
return lines.Select(l => projection(CsvFields(l, delim)));
}
public static IEnumerable<T> ProjectCsv<T>(this IEnumerable<string> lines, char[] delim, Func<string[], int, T> projection)
{
return lines.Select((l, i) => projection(CsvFields(l, delim), i));
}
static void Main(string[] args)
{
foreach (var filename in args)
{
var csv = ReadAllLines(filename);
var delimiter = new[] { ''t' };
var headers = CsvFields(csv.First(), delimiter);
Console.WriteLine(
new XDocument(new XElement("CSV",
new XAttribute("source", filename),
csv.ProjectCsv(delimiter, (fields, linenum) =>
new XElement("Line",
new XAttribute("number", linenum),
headers.Select((caption, index) => new XElement(caption, new XText(fields[index])))
))))
);
}
Console.WriteLine("Done, press a key");
Console.ReadKey();
}
}
}
用法:
Program.exe E:'test.csv E:'test2.csv
样本test.csv: aap noot mies
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
样本输出:<CSV source="e:'test.csv">
<Line number="0">
<aap>aap</aap>
<noot>noot</noot>
<mies>mies</mies>
</Line>
<Line number="1">
<aap>1</aap>
<noot>2</noot>
<mies>3</mies>
</Line>
<Line number="2">
<aap>2</aap>
<noot>3</noot>
<mies>4</mies>
</Line>
<Line number="3">
<aap>3</aap>
<noot>4</noot>
<mies>5</mies>
</Line>
<Line number="4">
<aap>4</aap>
<noot>5</noot>
<mies>6</mies>
</Line>
<Line number="5">
<aap>5</aap>
<noot>6</noot>
<mies>7</mies>
</Line>
<Line number="6">
<aap>6</aap>
<noot>7</noot>
<mies>8</mies>
</Line>
</CSV>
Done, press a key
这里给出一个简单易懂的解决方案。
文件input.csv
作为输入:
A,B,C
D,E,F
G,H
进程代码:
Program.cs
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace CSVtoXML
{
class Program
{
private static void AddContentForEachLine(string line, ref XElement xmlTree)
{
var currentTree = new XElement("Item");
const string delimiter = ","; // Can be changed based on the actual situation
string[] slices = line.Split(delimiter);
for (int i = 0; i < slices.Count(); i++)
currentTree.Add(new XElement($"Column{i}", slices[i].ToString()));
xmlTree.Add(currentTree);
}
static void Main(string[] args)
{
var basePath = Environment.CurrentDirectory;
var lines = File.ReadAllLines(Path.Combine(basePath, "../../..", @"input.csv"));
var xmlTree = new XElement("TopElement");
foreach (var line in lines)
{
AddContentForEachLine(line, ref xmlTree);
}
xmlTree.Save(Path.Combine(basePath, "../../..", @"output.xml"));
}
}
}
运行代码后,结果如下:
<?xml version="1.0" encoding="utf-8"?>
<TopElement>
<Item>
<Column0>A</Column0>
<Column1>B</Column1>
<Column2>C</Column2>
</Item>
<Item>
<Column0>D</Column0>
<Column1>E</Column1>
<Column2>F</Column2>
</Item>
<Item>
<Column0>G</Column0>
<Column1>H</Column1>
</Item>
</TopElement>
参考:在XML树中添加元素、属性和节点(c#) | Microsoft Docshttps://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/adding-elements-attributes-and-nodes-to-an-xml-tree