返回所有xml值并写入csv [英] Bring back all xml values and write to csv

查看:42
本文介绍了返回所有xml值并写入csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我目前正在尝试从xml文档中读取所有节点值。我用正确的值获取所有值,但我想写回我的csv和datatable的方式是我遇到问题的地方。



以下是我的代码:

  Dim  reader  As  XmlTextReader =  XmlTextReader(  C:\ftp \ upload\sessions.xml
Dim logwriter As StringBuilder
' Dim dt As New DataTable
' dt.Dispose()
Dim 作为 DataTable
table.Columns.Add( 报告 GetType String ))
table.Columns.Add( Session GetType String ))
table.Columns.Add( 访客 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))
(reader.Read())
选择 案例 reader.NodeType
案例 XmlNodeType.Element < span class =code-comment>' 显示元素的开头。
Console.Write( < + reader.Name)
如果 reader.HasAttributes 然后 ' 如果存在属性
< span class =code-keyword> while reader.MoveToNextAttribute()
' 显示属性名称和价值。
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)
结束 while
结束 如果
控制台。 WriteLine( >
案例 XmlNodeType.Text ' 显示每个元素中的文本。
Console.WriteLine (reader.Value)
logwriter.AppendLine(reader.Value + c)
案例 XmlNodeType.EndElement ' 显示结束元素。
Console.Write( br mode = hold /> Console.WriteLine(>

结束选择
循环

使用LOG作为新的StreamWriter(
C:\ftp \ upload \XMLTEST.csv < span class =code-string>)
LOG.WriteLine(logwriter.ToString)
'dt.ToString()
logwriter = Nothing
LOG.Dispose()
LOG.Close()
结束使用



我希望它在数据表中的方式和csv是colum 1必须包含重复数据一直到xml父节点结束。第二个列必须包含第二个节点的值,依此类推,使其成为具有一对多关系的层次结构表。



所以例如:



ParentNode Value

  info  2013-10-22T08:36:22 + 00:00   现在 已连接   < span class =code-leadattribute> Amelia  
Amelia 2013-10-22T08:36:29 + 00:00 ID48
2013-10-22T08:37:07 + 00:00 leanne
Amelia 2013-10-22T08:37:32 + 00:00 ID48
2013-10-22T08:37:47 + 00:00 非常 well 谢谢









有没有这样做?

解决方案

请参阅下面的链接

链接1 [ ^ ]

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:

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

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?

解决方案

Refer Below Link
Link 1 [^]


这篇关于返回所有xml值并写入csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆