Convert DataTable XML to CSV Using XSL 
Author Message
 Convert DataTable XML to CSV Using XSL

Being totally new to the whole XSL thing, anyone have any code snippets or
ideas on how I might approach this?

Thanks!

Mark



Tue, 22 Nov 2005 06:28:39 GMT  
 Convert DataTable XML to CSV Using XSL
Yeah, why?

XML to CSX via XLS

Do you like hurting yourself?


Quote:
> Being totally new to the whole XSL thing, anyone have any code snippets or
> ideas on how I might approach this?

> Thanks!

> Mark



Tue, 22 Nov 2005 08:02:49 GMT  
 Convert DataTable XML to CSV Using XSL
'Course I do but what's that got to do with it ;-)

Seriously though, I get paid to do as I'm told, and I've been told certain
clients want access to the underlying data in CSV format, I don't wanna do
it but ......

I could interate the DT and write out the details, but I've after something
more exciting!

Mark



: Yeah, why?
:
: XML to CSX via XLS
:
: Do you like hurting yourself?
:


: > Being totally new to the whole XSL thing, anyone have any code snippets
or
: > ideas on how I might approach this?
: >
: > Thanks!
: >
: > Mark
: >
: >
:
:



Tue, 22 Nov 2005 09:06:20 GMT  
 Convert DataTable XML to CSV Using XSL
Well, the exciting news is....there is no exciting news.

I'd beeter explain and help otherwise you're going to get ticked.

Okay, XML>>XLS is a means by which you reform your XML into HTML.. At least,
that's the normal bent.

So if you're looking for some Hail Marys, you may want to move to the Out
Fathers.

Yet a table by any other name is a table, right?

Here's the code:

       Dim ds As New DataTable()
        Dim r As DataRow
        Dim x As Integer
        Dim y As Integer
        Dim csv As String

        Dim FSO As Object
        Dim txtStream As Object
        FSO = CreateObject("Scripting.FileSystemObject")
        txtStream = FSO.OpenTextFile("C:\temp.csv", 2, True, -2)

        dt = Datagrid1.Datasource

        r = dt.Tables(0).Rows(0)
        For x = 0 To dt.Tables(0).Columns.Count - 2

            csv = csv & dt.Tables(0).Columns(x).Caption & ","
        Next

        csv = csv & dt.Tables(0).Columns(x).Caption
        txtStream.Writeline(csv)

        csv = ""

        For x = 0 To dt.Tables(0).Rows.Count - 1

            r = dt.Tables(0).Rows(x)

            For y = 0 To dt.Tables(0).Columns.Count - 2

                If (r(y)) Is Nothing = True Then
                    csv = csv & ","
                Else
                    csv = csv & r(y) & ","
                End If

            Next

            If (r(y)) Is Nothing = True Then
                csv = csv & ","
            Else
                csv = csv & r(y)
            End If
            txtStream.WriteLine(csv)
            csv = ""
        Next

        txtStream.close()
        txtStream = Nothing

        FSO = Nothing

Thus are the words of todays holly grail.

Sincerely hope this helps. If it doesn't, I get down on my hands and knees
and beg for mercy.


Quote:
> 'Course I do but what's that got to do with it ;-)

> Seriously though, I get paid to do as I'm told, and I've been told certain
> clients want access to the underlying data in CSV format, I don't wanna do
> it but ......

> I could interate the DT and write out the details, but I've after
something
> more exciting!

> Mark



> : Yeah, why?
> :
> : XML to CSX via XLS
> :
> : Do you like hurting yourself?
> :


> : > Being totally new to the whole XSL thing, anyone have any code
snippets
> or
> : > ideas on how I might approach this?
> : >
> : > Thanks!
> : >
> : > Mark
> : >
> : >
> :
> :



Tue, 22 Nov 2005 10:27:07 GMT  
 Convert DataTable XML to CSV Using XSL
Mark,
You can use XSL to convert XML to CSV. I don't have a sample handy, but I
know you will need to use <xsl:output method="text"> directive. My concern
with using XSL is making it general enough to handle any xml, I know it can
be done, I just haven't jumped that deep into it.

Heres a quick VB.NET 1.1 export routine that is very general (too general?):

    ' Required imports
    Imports System.IO    ' for the StreamWriter
    Imports System.Text ' for the UnicodeEncoding

    ' sample usage
    Export("Customers.csv", DataSet1.Tables("Customers"))
    Export("Employees.csv", DataSet1.Tables("Employees"))

    Public Sub Export(ByVal path As String, ByVal table As DataTable)
        Dim output As New StreamWriter(path, False, UnicodeEncoding.Default)
        Dim delim As String

        ' Write out the header row
        delim = ""
        For Each col As DataColumn In table.Columns
            output.Write(delim)
            output.Write(col.ColumnName)
            delim = ","
        Next
        output.WriteLine()

        ' write out each data row
        For Each row As DataRow In table.Rows
            delim = ""
            For Each value As Object In row.ItemArray
                output.Write(delim)
                If TypeOf value Is String Then
                    output.Write(""""c) ' thats four double quotes and a c
                    output.Write(value)

                    output.Write(""""c) ' thats four double quotes and a c
                Else
                    output.Write(value)
                End If
                delim = ","
            Next
            output.WriteLine()
        Next

        output.Close()

    End Sub

You can change (or remove) the Encoding parameter above to suit your needs,
I used Unicode as the table had non ASCII characters in it. Also when
writing strings, I don't deal with double quotes in the string. If you make
the StreamWriter a parameter it will be much more flexible (you could go to
a memory stream to support cut & paste). I use the default formatting for
numeric types.

Hope this helps
Jay


Quote:
> Being totally new to the whole XSL thing, anyone have any code snippets or
> ideas on how I might approach this?

> Thanks!

> Mark



Tue, 22 Nov 2005 23:49:17 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. using the excel 10 lib to convert xml to csv

2. Render XML+XSL into PrintDocument (not XML) ?

3. SAX XML CSV converter using visual basic 6

4. XML-XSL Transformation

5. render Hello.xml + Hello.xsl into PrintDocument ?

6. xml in asp and xsl

7. VB and XML/XSL

8. Viewing a XML dokument with corresponding XSL and CSS

9. Create xml/xsl file

10. FREE Tutorials on HTML XHTML CSS JavaScript XML XSL ASP SQL ADO VBScript, SAP - ABAP

11. XML / XSL -- PPC 2002 -- An interesting Query ??!!

12. DataTable to CSV file

 

 
Powered by phpBB® Forum Software