Parse semicolon separated csv string into list object

If your CSV is semicolon-separated instead of comma-separated, you can modify the code accordingly. Here's an example:

This post is about how to read comma separated csv data in C#.

Note: If your CSV is semicolon-separated instead of comma-separated, you can modify the code accordingly.


private List ReadCsvString(string csvString)
        {
            List result = new List();

            using (TextFieldParser parser = new TextFieldParser(new System.IO.StringReader(csvString)))
            {
                parser.SetDelimiters(",");

                // Skip the header as CSV string has one
                if (!parser.EndOfData)
                {
                    parser.ReadLine();
                }

                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();

                    // Assuming your CSV should be 10 columns (adjust as needed)
                    if (fields.Length == 10)
                    {
                        AddressSiteRoutingCSV csvItem = new AddressSiteRoutingCSV
                        {
                            SiteId = fields[0],
                            ContactCompany = fields[1],
                            Name = fields[2],
                            Address1 = fields[3],
                            Address2 = fields[4],
                            Address3 = fields[5],
                            Address4 = fields[6],
                            PostalCode = fields[7],
                            DelRunDepot = fields[8],
                            Run = fields[9]
                        };

                        result.Add(csvItem);
                    }
                   
                }
            }


            return result;
        }
 

Experienced Software Developer with a demonstrated history of working in the Information Technology and services industry. Strong engineering professional skilled in DotVVM, AutoDesk Forge API's, C#, ASP.NET MVC, SQL Server, Angular,React JS, ASP.NET Core, Web API, Javascript, Jquery, Windows forms, WPF, ASP.NET WebForms, Nopcommerce, Python, Azure Open AI, Azure DevOps.

To start an initial chat, just drop me an email at prince@debugtutorial.com or use the form on the contact page.