I am reading a CSV file and converting it into XML format.
public static string ConvertCSVToXML(string filePath)
{
var lines = File.ReadAllLines(filePath);
XElement xml = new XElement("RootElement",
from str in lines
let col = str.Split(',')
select new XElement("Item",
new XElement("Column1", col[0]),
new XElement("Column2", col[1]),
new XElement("Column3", col[2]),
new XElement("Column4", col[3])
)
);
return xml.ToString();
}
Here we know the header of the CSV so it works. How to convert it when schema of the CSV is undefined? File contains header row but it is not known how many headers and what are header titles.