I need to convert a CSV to an XML document. All the examples I have seen so far show that how to do this with certain columns in CSV.
I still have been using LINQ:
string [] file = file. Read Line (@ "C: \ text CSV"); String xml = ""; New excelment ("item", new excelment ("column 1", field [0]), new excelment (" New XElement ("Column4", Field [3]), New XElement ("Column5", Field [4])); New XElement ("Column 3", Field [2]), File. Light All Lessons (@ "C : \ Xmlout.xml ", xml + top.ToString ());
This is for a certain amount, but there are different columns on each row in my CSV.
How does the car loop fit, depending on how many words (columns) are there in each line of the .CSV?
Thnx
var rows = file. ReadAlline (@ "C: \ text.csv"); xml = new XElement ("TopElement", lines.Select (line = & Gt; New XElement ("item", line.Split (',') .Select ((column, index) => New XElement ("column" + index, column)))); xml.Save (@ "C: \ xmlout.xml");
Input:
A, B, C, E, FG, H < / Code>
Output:
< Urw> & lt; Top element & gt; & Lt; Item & gt; & Lt; Column 0 & gt; & Lt; / Column0 & gt; & Lt; Column 1 & gt; B & lt; / Column1 & gt; & Lt; Column2 & gt; C & lt; / Column2 & gt; & Lt; / Item & gt; & Lt; Item & gt; & Lt; Column 0 & gt; D & lt; / Column0 & gt; & Lt; Column 1 & gt; E & lt; / Column1 & gt; & Lt; Column2 & gt; F. & lt; / Column2 & gt; & Lt; / Item & gt; & Lt; Item & gt; & Lt; Column 0 & gt; G & lt; / Column0 & gt; & Lt; Column 1 & gt; H & lt; / Column1 & gt; & Lt; / Item & gt; & Lt; / TopElement & gt;
Comments
Post a Comment