Home > Code Samples > Convert XML to CSV in VB
XML to CSV
Source File Format
<?xml version="1.0" encoding="utf-8" ?>
<users>
<user>
<user_id>1</user_id>
<first_name>George</first_name>
<last_name>Washington</last_name>
</user>
<user>
<user_id>2</user_id>
<first_name>Abraham</first_name>
<last_name>Lincoln</last_name>
</user>
...
</users>
Destination File Format
user_id,first_name,last_name
1,George,Washington
2,Abraham,Lincoln
...
VB.Net
Dim writer As New CsvWriter("users.csv")
Dim reader As New XmlRecordReader("users.xml", "users/user")
reader.Columns.Add("user_id", "id")
reader.Columns.Add("first_name", "first")
reader.Columns.Add("last_name", "last")
writer.Write("user_id")
writer.Write("first_name")
writer.Write("last_name")
writer.EndRecord()
While reader.ReadRecord()
writer.Write(reader.Item("id"))
writer.Write(reader.Item("first"))
writer.Write(reader.Item("last"))
writer.EndRecord()
End While
reader.Close()
writer.Close()
|