VB.NET - Create a CSV file from an XML file?



This code will indeed create an Excel .csv file, but it's not a true CSV. All that will be contained in it is some gobbledygook which VB creates from dgvStats.Rows.ToString. I'm guessing that some kind of a For...Each is needed here, but I can't see how I would work that out.


As a side matter: I already have the query written in an earlier Sub, but if I try to give it global scope (in the Declarations section), the Subs access the query but throw a MissingMemberException. Is there some way I can do this without writing the query twice?


Thanks for your help!



Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
Dim StatsData As XElement = XElement.Load("Basketball.xml")
Dim query = From p In StatsData.Descendants("player")
Let name = p.<name>.Value
Let team = p.<team>.Value
Let points = CInt(p.<points>.Value)
Let steals = CInt(p.<steals>.Value)
Order By points Descending
Select name, team, points, steals

Dim sw As IO.StreamWriter = IO.File.CreateText("Basketball.csv")
sw.WriteLine(dgvStats.Rows.ToString)
sw.Close()
MessageBox.Show("Basketball.csv created in bin\Debug folder. " &
"Use Windows Explorer to find it.")
End Sub

No comments:

Post a Comment