Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am currently trying to read all node values from an xml document. I get all the values with the correct values but the way i want to write back to my csv and datatable is where I am having problems.

Below is my code:
VB.NET
Dim reader As XmlTextReader = New XmlTextReader("C:\ftp\upload\sessions.xml")
        Dim logwriter As New StringBuilder
        'Dim dt As New DataTable
        'dt.Dispose()
        Dim table As New DataTable
        table.Columns.Add("Report", GetType(String))
        table.Columns.Add("Session", GetType(String))
        table.Columns.Add("Visitor", GetType(String))
        table.Columns.Add("ip", GetType(String))
        table.Columns.Add("agent", GetType(String))
        table.Columns.Add("host", GetType(String))
        table.Columns.Add("chatReferer", GetType(String))
        table.Columns.Add("GeoInfo", GetType(String))
        table.Columns.Add("Chat", GetType(String))
        table.Columns.Add("line", GetType(String))
        table.Columns.Add("Text", GetType(String))
        table.Columns.Add("HTML", GetType(String))
        'table.Columns.Add("Session", GetType(String))
        Do While (reader.Read())
            Select Case reader.NodeType
                Case XmlNodeType.Element 'Display beginning of element.
                    Console.Write("<" + reader.Name)
                    If reader.HasAttributes Then 'If attributes exist
                        While reader.MoveToNextAttribute()
                            'Display attribute name and value.
                            Console.Write(" {0}='{1}'", reader.Name, reader.Value)
                            'logwriter.Append(reader.Name + ","c)
                            'table.Columns.Add(reader.Name)
                            'table.Rows.Add(reader.Value + ","c)
                            logwriter.Append(reader.Value + ","c)
                        End While
                    End If
                    Console.WriteLine(">")
                Case XmlNodeType.Text 'Display the text in each element.
                    Console.WriteLine(reader.Value)
                    logwriter.AppendLine(reader.Value + ","c)
                Case XmlNodeType.EndElement 'Display end of element.
                    Console.Write("br mode="hold" />                    Console.WriteLine(">")

            End Select
        Loop

        Using LOG As New StreamWriter("C:\ftp\upload\XMLTEST.csv")
            LOG.WriteLine(logwriter.ToString)
            'dt.ToString()
            logwriter = Nothing
            LOG.Dispose()
            LOG.Close()
        End Using

the way I want it in the datatable and csv is that colum 1 must contain repetitive data all the way down till the end of the xml parent node. The second colum must contain the value of the 2nd node and so on to make it into a hierarchy table with one to many relationship.

so for example:

ParentNode Value
CSS
info    2013-10-22T08:36:22+00:00   You are now connected with Amelia
Amelia  2013-10-22T08:36:29+00:00   ID48
You 2013-10-22T08:37:07+00:00   leanne
Amelia  2013-10-22T08:37:32+00:00   ID48
You 2013-10-22T08:37:47+00:00   very well thanks




Is there anyway of doing this?
Posted
Updated 30-Oct-13 23:05pm
v2

1 solution

Refer Below Link
Link 1 [^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900