[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

CSV to XML in VB.Net

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>
  • 4
  • 4
1 Solution
käµfm³d 👽Commented:
What is your question?
MonaglitAuthor Commented:
I need to convert the above CSV file to XML in VB.Net, need help with the coding. Thanks.
käµfm³d 👽Commented:
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

käµfm³d 👽Commented:
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.
MonaglitAuthor Commented:
Thanks a lot. Works very well.
MonaglitAuthor Commented:
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.
käµfm³d 👽Commented:
I'm confused. You selected your own comment as the answer?
MonaglitAuthor Commented:
Sorry, yes I am new..

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now