Reputation: 11
i know the basic for converting a CSV file to an XML but my CSV files has different columns and i also need it to split it differently.How am i able to use C# and convert my CSV file to this XML file? thanks for your help!
<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());
}
There is more than a hundred data BUT with different amount of columns and rows needed to be import to the XML from CSV.please help! this is the codes i've been using so far,but it wont works for me as i dun have a certain row, some data i need 7 rows some i need 9 or 10.plus i need to create a new category like < NAME ="bbk" > and put in data in it. thanks for your help.
My CSV sourse code eg is amk, A1,12.00,11.59,PARK,DAY,abc,77 A1,01.00,12.59,garden,night,abc,27,2.15M,12 bbk, B1,23.59,11.59,Rooftop,DAY,abc,34 B4,01.00,02.00,garden,Day,abc,27,2.00M,2 Please tell me how i need to change my CSV code to let my desired XML output to be working. thanks!
Upvotes: 1
Views: 4854
Reputation: 9888
Here gives a simpler solution, easy to understand.
The file input.csv
as input:
A,B,C
D,E,F
G,H
The code for process:
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"));
}
}
}
After running the code, the result is below:
<?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>
Reference:
Adding Elements, Attributes, and Nodes to an XML Tree (C#) | Microsoft Docs https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/adding-elements-attributes-and-nodes-to-an-xml-tree
Upvotes: 0
Reputation: 393829
Something to get you started:
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();
}
}
}
Usage:
Program.exe E:\test.csv E:\test2.csv
Sample test.csv:
aap noot mies
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
Sample output:
<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
Upvotes: 5