Convert CSV file to XML

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I've a simple windows application and i want to be able to convert a csv
file to xml. So far, i can browse for the csv file from the file structure
and display in a textbox. I want to convert the displayed csv file in the
textbox to Xml and use the first line of the csv file as tags. I know there
will be different ways of doing this but i would like to keep it simple
because i am new to programming and the Visual Basic.Net environment. Any
help will be much appreciated.

Sample Input.csv

"Title", "Firstname", "Surname"
"Miss", "Lisa", "Simpson"
"Mr", "Joe", "Bloggs"
"Mrs", "Maggie", "Simpson"

Sample output.xml

<Data>
<Address>
<Title>Miss</Title>
< Firstname>Lisa</ Firstname>
< Lastname>Simpson</ Lastname>
</Address>
</Data>


Thanks........
 
Ish2000,
Have you looked at the XmlCsvReader?

http://www.gotdotnet.com/Community/...mpleGuid=DDE579F1-836A-4562-A3CB-57A7DBBEBAE0

It reads a formatted file (such as CSV) as "Xml", this Xml should then be
easily parsed into your string1, integer1, double1 variables...

The "easiest" way might be to use XmlCsvReader to read the CSV into an XML
document, then use a normal XmlTextWriter to save the XML document to a
file.

Alternatively you should be able to do an identity transform with an XSLT
Transformation:

<xsl:transform version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='@*|node()' >
<xsl:copy>
<xsl:apply-templates select='@*|node()'/>
</xsl:copy>
</xsl:template>
</xsl:transform>

The above transformation is from
http://msdn.microsoft.com/msdnmag/issues/0800/xslt/

Hope this helps
Jay
 
Hi,

This code will create a csv file, load it into a dataset, and save
it as an xml file.


If Not Directory.Exists("C:\CSV Test") Then
Directory.CreateDirectory("C:\CSV Test")

'

' Create a csv file

'

Dim sw As New StreamWriter("C:\CSV Test\Test.csv", False)

sw.WriteLine("Column1,Column2,Column3")

For x As Integer = 0 To 20

sw.WriteLine("{0},{0},{0}", x)

Next

sw.Close()

'

' Open an oledb connection and show it in a datagrid

'

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\CSV
Test;Extended Properties=""text;HDR=Yes;FMT=Delimited"""

Dim conn As New OleDb.OleDbConnection(strConn)

Dim da As OleDb.OleDbDataAdapter

Dim ds As New DataSet

Dim mycmd As New OleDb.OleDbCommand("Select * from Test.csv", conn)

Try

da = New OleDb.OleDbDataAdapter(mycmd)

da.Fill(ds)

Catch ex As Exception

Trace.WriteLine(ex.ToString)

Return

End Try

DataGrid1.DataSource = ds.Tables(0)

DataGrid1.CaptionText = "CSV Test"

ds.WriteXml("C:\CSV Test\CSV.xml")



Ken

------------------------------

Hello,

I've a simple windows application and i want to be able to convert a csv
file to xml. So far, i can browse for the csv file from the file structure
and display in a textbox. I want to convert the displayed csv file in the
textbox to Xml and use the first line of the csv file as tags. I know there
will be different ways of doing this but i would like to keep it simple
because i am new to programming and the Visual Basic.Net environment. Any
help will be much appreciated.

Sample Input.csv

"Title", "Firstname", "Surname"
"Miss", "Lisa", "Simpson"
"Mr", "Joe", "Bloggs"
"Mrs", "Maggie", "Simpson"

Sample output.xml

<Data>
<Address>
<Title>Miss</Title>
< Firstname>Lisa</ Firstname>
< Lastname>Simpson</ Lastname>
</Address>
</Data>


Thanks........
 
Ken,

That first part of your sample is in my opinion confusing, I was almost
giving a sample, when I saw that you in the middle started with the sample
that would have been almost the same as mine.

(as well meant for the OP of course)

Cor
 
Hi,

Sorry.

Ken
----------------
Ken,

That first part of your sample is in my opinion confusing, I was almost
giving a sample, when I saw that you in the middle started with the sample
that would have been almost the same as mine.

(as well meant for the OP of course)

Cor
 
Hi Ken,

My message was only to make you (and the OP) attent on it

Nothing to "Sorry" in my opinion, for you only for the next time, to make it
even better than it was.

:-)

Cor
 
Hi Jay,

Thanks for replying to my question. I've had a look at XmlCsvReader but the
source code is in C#. Can i get an XmlCsvReader in Visual Basic.Net?

thanks.....
 
Ish2000,
Can i get an XmlCsvReader in Visual Basic.Net?
Not that I know of. However! I have to ask: Why would you *really* need it
in C#? (Remember, at the end of the day, both C# & VB.NET are simply
languages built upon .NET itself!)

Quickly looking at the package, you should be able to run the exe "As Is" to
convert a CSV to an XML document. (via Process.Start for example)

If you want to call the class directly yourself, I would recommend packaging
the XmlCsvReader class & any support classes in a C# class library assembly,
then referencing this assembly from your VB.NET project.

If you still really want VB.NET source there are any number of C# to VB.NET
converters available on the net.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top