Hello. Implemented the conversion as follows:
using System.Xml.Linq; XElement xmlf = new XElement("Folder", new XElement("name", "NASI PARTNERZY HANDLOWI"), from str in source let fields = str.Split(';') select new XElement("Placemark", new XElement("name", fields[0]), new XElement("description", fields[1]), new XElement("styleUrl", fields[2]), new XElement("ExtendedData", new XElement("Data", new XAttribute("name", "opis"), new XElement("value", fields[3])), new XElement("Data", new XAttribute("name", "Informacje:"), new XElement("value", fields[4])), new XElement("Data", new XAttribute("name", "Telefon:"), new XElement("value", fields[5])), new XElement("Data", new XAttribute("name", "E-mail"), new XElement("value", fields[6])), new XElement("Data", new XAttribute("name", "Strona:"), new XElement("value", fields[7])), new XElement("Data", new XAttribute("name", "gx_media_links"), new XElement("value", fields[8]))), new XElement("Point", new XElement("coordinates", fields[9])))); In the end, I get the right structure, but filled with incorrect data. Example structure:
<Folder> <name>Company</name> <Placemark> <name>Company name</name> <description>Some description</description> <styleUrl>#icon-503-DB4436</styleUrl> <ExtendedData> <Data name='opis'> <value>Som desc.</value> </Data> <Data name='Informacje:'> <value>Some information </value> </Data> <Data name='Telefon:'> <value>(22) 333 55 55 (22)</value> </Data> <Data name='E-mail'> <value>qwerty@mail.com</value> </Data> <Data name='Strona:'> <value>www.google.com</value> </Data> <Data name='gx_media_links'> <value>some links</value> </Data> </ExtendedData> <Point> <coordinates>22.912227,52.12221,0.0</coordinates> </Point> </Placemark> </Folder> The reason for the incorrect filling in the excessively saturated text with the element; , is it possible, based on the current code, to implement the correct conversion? Or what implementation options should be used in this situation.
Update
I will give the full version of the code to clarify the correctness of reading CSV using CSVHelpera (I have a problem with output to XML, only one line is output from the CSV file):
using (var sr = new StreamReader(Fd2.FileName)) { var reader = new CsvReader(sr); IEnumerable<DataRecord> records = reader.GetRecords<DataRecord>(); foreach (DataRecord record in records) // (int element in fibarray) (var record in DataRecord) { XElement category = new XElement("Folder", new XElement("name", "NASI PARTNERZY HANDLOWI"), new XElement("Placemark", new XElement("name", record.name), new XElement("description", record.opis + "<br><br>Informacje: " + record.information + "<br>Telefon: " + record.telefon + "<br>E-mail: " + record.mail + "<br>Strona: " + record.strona + "<br>" + record.img), new XElement("styleUrl", record.icon), new XElement("ExtendedData", new XElement("Data", new XAttribute("name", "gx_media_links"), new XElement("value", record.gx))), new XElement("Point", new XElement("coordinates", record.cordinates)))); using (var sw2 = new StreamWriter(xmlFile, false, Encoding.UTF8)) sw2.WriteLine(category); } }
StreamWriterconstructor: you specifiedfalseas the second parameter, i.e., you have forbidden it to append to the old file. Here it is, each time and overwrites the previous line with a new one. - VladD