473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Convert CSV file to XML

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........
Nov 21 '05 #1
7 15002
Ish2000,
Have you looked at the XmlCsvReader?

http://www.gotdotnet.com/Community/U...B-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
"Ish2000" <Is*****@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
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........

Nov 21 '05 #2
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

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

"Ish2000" <Is*****@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
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........

Nov 21 '05 #3
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
Nov 21 '05 #4
Hi,

Sorry.

Ken
----------------
"Cor Ligthert" <no************@planet.nl> wrote in message
news:Ow**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #5
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
Nov 21 '05 #6
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.....

"Jay B. Harlow [MVP - Outlook]" wrote:
Ish2000,
Have you looked at the XmlCsvReader?

http://www.gotdotnet.com/Community/U...B-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
"Ish2000" <Is*****@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
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........


Nov 21 '05 #7
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
"Ish2000" <Is*****@discussions.microsoft.com> wrote in message
news:6E**********************************@microsof t.com... 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.....

"Jay B. Harlow [MVP - Outlook]" wrote:
Ish2000,
Have you looked at the XmlCsvReader?

http://www.gotdotnet.com/Community/U...B-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
"Ish2000" <Is*****@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
> 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........
>
>


Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
1
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a...
7
by: Scott Schluer | last post by:
Is there a way to use the Image class to convert a color photo (GIF or JPEG) to a B&W photo? Thanks, Scott
3
by: Thubaiti | last post by:
Hi, I have this code in my ASP.NET and I want to convert it to C# (code behind) <asp:Repeater id="subCategoryRepeater" runat="server"> <ItemTemplate> <ul> <li> <asp:HyperLink...
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
5
by: melickas | last post by:
We designed a custom application using Office Developer Tools '97 which included a Run-time version of Access--- so it would not matter if our customer even had any version of Access on their...
6
by: PenguinPig | last post by:
Dear All Experts I would like to know how to convert a HTML into Image using C#. Or allow me contains HTML code (parsed) in Image? I also tried this way but it just display the character "<" &...
5
by: sonu | last post by:
hey good morning ...... how to convert a video file in .flv format in php for linux hosting......is there any package whis provide this facility . Can i use ffmpeg for linux hosting...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.