conversion table from csv to xml

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mones
    New Member
    • Jul 2012
    • 1

    conversion table from csv to xml

    hello
    I am beginner in developing C # and I will like to develop an application that a converte. csv file (table) in an xml file
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    You can use the XElement within LINQ to accomplish this. Here's a sample of what I'm talking about:

    Code:
    public XElement GetCustomers()
    {
        string[] csvFile = File.ReadAllLines("sample.csv");
        var customer = new XElement("Root",
                                            from @string in csvFile
                                            let fields = @string.Split(',')
                                            select new XElement("Customers",
                                                new XAttribute("CustomerID", fields[0]),
                                                new XElement("CompanyName", fields[1]),
                                                new XElement("ContactName", fields[2]),
                                                new XElement("ContactTitle", fields[3]),
                                                new XElement("Phone", fields[4])));
        return customer;
    }
    Hope that helps

    NOTE: My sample csv file was comma delimited. If you're is a different character then split on your delimiter.
    Last edited by PsychoCoder; Jul 9 '12, 10:36 PM.

    Comment

    Working...