CSV to XML in VB.Net

Posted on 2011-10-22
Last Modified: 2013-11-26
The CSV file can have multiple records in it. The first row will contain the column headers for each column. The XML should contain an auto-generated row ID for each record in the CSV file.
For example :
Input CSV file
The resultant XML file should be
<Records filepath=C:\input.csv>
        <Record RowID=1>
        <Record RowID=2>
Question by:Monaglit
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    What is your question?

    Author Comment

    I need to convert the above CSV file to XML in VB.Net, need help with the coding. Thanks.
    LVL 74

    Accepted Solution

    Here's one approach:

    Module Module1
        Sub Main()
            Dim csvPath As String = "input.csv"
            Dim parser As New FileIO.TextFieldParser(csvPath)
            Dim xdoc As New Xml.XmlDocument()
            Dim root As Xml.XmlNode = xdoc.AppendChild(xdoc.CreateElement("Records"))
            Dim currentRecord As Integer = 0
            root.Attributes.Append(xdoc.CreateAttribute("filepath")).InnerText = csvPath
            parser.TextFieldType = FileIO.FieldType.Delimited
            parser.HasFieldsEnclosedInQuotes = True
            parser.Delimiters = New String() {","}
            Dim headers() As String = parser.ReadFields()
            While Not parser.EndOfData
                Dim record As Xml.XmlNode = root.AppendChild(xdoc.CreateElement("Record"))
                Dim fields() As String = parser.ReadFields()
                currentRecord += 1
                record.Attributes.Append(xdoc.CreateAttribute("RowID")).InnerText = currentRecord.ToString()
                For i As Integer = 0 To headers.Count - 1
                    record.AppendChild(xdoc.CreateElement(headers(i))).InnerText = fields(i)
            End While
        End Sub
    End Module

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    You don't have to use the HasFieldsEnclosedInQuotes = True option if your CSV doesn't have such fields; you didn't specify, so I left in to be safe.

    Author Comment

    Thanks a lot. Works very well.

    Author Comment

    I've requested that this question be closed as follows:

    Accepted answer: 0 points for Monaglit's comment http:/Q_27410156.html#37011622

    for the following reason:

    Exactly what I was looking for. Works just right.
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    I'm confused. You selected your own comment as the answer?

    Author Closing Comment

    Sorry, yes I am new..

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    Shoutout to Emily Plummer ( for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now