Home > Back-end >  Convert CSV in XML file with C#
Convert CSV in XML file with C#

Time:10-14

I wrote this piece of code that allows me to read a CSV file and convert it to an XML file. I have a problem, if inside the CSV file there are semicolons (;) the program cannot read the data instead, if there are commas (,) that delimit the words the program can read the data and to insert them correctly in the XML file. could you find a way to replace the semicolon (;) with the comma (,)? Thank you very much!! :)

This is the code:

        writer.WriteStartDocument();
        writer.WriteStartElement("DC8_Recipes");

        using (CsvReader reader = new CsvReader(path))
        {
            reader.ReadHeaders();

            while (reader.ReadRecord())
            {
                writer.WriteStartElement("DC8_Recipes");

                writer.WriteAttributeString("PlantNo", reader["id_imp"]);
                writer.WriteAttributeString("No", reader["nome_frm"]);
                writer.WriteAttributeString("Name", reader["desc_frm"]);

                writer.WriteEndElement();
            }
            reader.Close();
        }

        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Close();


        logText.Text  = DateTime.Now   " Convertion Completed\n";
        logText.Text  = DateTime.Now   " Saving file to: "   savepath   "\n";
        try
        {
            logText.Text  = DateTime.Now   " File save completed!\n";
            logText.Text  = DateTime.Now   " process END\n";
        }
        catch
        {

        }
    }

CodePudding user response:

You can pass into CsvReader constructor a CsvConfiguration to change the default delimiter (which is based on the current CultureInfo):

The culture is used to determine the default delimiter, default line ending, and formatting when type converting.

using (var csv = new CsvReader(writer, new CsvConfiguration(CultureInfo.InvariantCulture) 
{ 
    Delimiter = "," 
}))
{
    csv.Read();
}

CodePudding user response:

You could write your own CsvReader:

    public static List<Model> ReadCsv(string path)
    {
        var modelList = new List<Model>();
        using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            using (var streamReader = new StreamReader(fileStream, Encoding.Default))
            {
                while (!streamReader.EndOfStream)
                {
                    var line = streamReader.ReadLine();

                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    var splittedLine = line.Split(';');

                    var model = new Model();

                    for (var i = 0; i < splittedLine.Length; i  )
                    {
                        switch (i)
                        {
                            case 0:
                                model.FirstColumn = splittedLine[i];
                                break;
                            case 1:
                                model.SecondColumn = splittedLine[i];
                                break;
                            case 2:
                                model.ThirdColumn = Convert.ToInt32(splittedLine[i]);
                                break;
                        }
                    }

                    modelList.Add(model);
                }
            }
        }

        return modelList;
    }
  • Related