Read an Xml, load it into a DataTable and go through it in Visual Basic .net 2008

1

You will see I'm trying to read an XML file, mount it in a DataTable and then go through the DataTable, but I can not do it and I've been trying it for a while, for this I'm using the following code I found in msdn:

introducir el código aquí
Module Module1

Sub Main()

End Sub

Private Sub DemonstrateReadWriteXMLDocumentWithReader()
    Dim table As DataTable = CreateTestTable("dt")

    table.ReadXml(Environment.CurrentDirectory & "\xmlFile.xml")
    Stop
    PrintValues(table, "Original table")
    Stop
    ' Write the schema and data to XML in a memory stream.

    Dim xmlStream As New System.IO.MemoryStream()
    table.WriteXml(xmlStream, XmlWriteMode.WriteSchema)

    ' Rewind the memory stream.
    xmlStream.Position = 0

    Dim reader As New System.Xml.XmlTextReader(xmlStream)
    Dim newTable As New DataTable
    newTable.ReadXml(reader)

    ' Print out values in the table.
    PrintValues(newTable, "New Table")
    Stop
End Sub

Private Function CreateTestTable(ByVal tableName As String) As DataTable
    ' Create a test DataTable with two columns and a few rows.
    Dim table As New DataTable(tableName)
    Dim column As New DataColumn("nombre", GetType(System.String))
    column.AutoIncrement = True
    table.Columns.Add(column)

    column = New DataColumn("total", GetType(System.String))
    table.Columns.Add(column)

    column = New DataColumn("Telf_Cel", GetType(System.String))
    table.Columns.Add(column)

    ' Add ten rows.
    'Dim row As DataRow
    'For i As Integer = 0 To 9
    '   row = table.NewRow()
    '   row("item") = "item " & i
    '   table.Rows.Add(row)
    'Next i

    table.AcceptChanges()
    Return table
End Function

Private Sub PrintValues(ByVal table As DataTable, ByVal label As String)
    Console.WriteLine(label)
    For Each row As DataRow In table.Rows
        For Each column As DataColumn In table.Columns
            Console.Write("{0}{1}{2}", ControlChars.Tab, row(column))
        Next column
        Console.WriteLine()
        Stopwatch.
    Next row
End Sub
End Module

This is my XML

 <?xml version="1.0" 
 standalone="yes"?> 
 <acueductosDataSet 
 xmlns="tempuri.org/
 acueductosDataSet.xsd">; 
<Consultar_Cel_y
_MontosPendientes> 
<nombre>JOSE LUIS BLANCO 
ARAYA</nombre> 
<total>4851.6</total> 
<Telf_Cel>85382021
</Telf_Cel> 
</Consultar_Cel_y
  _MontosPendientes> 
  <Consultar_Ce
   _y_MontosPendientes> 
       <nombre>
       MARIA FELICIA 
 GRANADOS 
 RODRIGUEZ</nombre> 
 <total>5125</total> 
<Telf_Cel>85382021
</Telf_Cel> 
</Consultar_Cel_
 y_MontosPendientes> 
</acueductosDataSet>
    
asked by maikmr 28.04.2018 в 00:36
source

1 answer

1

To read XML des visual.net you can do it this way is an example with a button the address is by default and read each parent and child

Imports System.Xml
Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xmldoc As New XmlDataDocument()
        Dim xmlnode As XmlNodeList
        Dim i As Integer
        Dim str As String
        Dim fs As New FileStream("products.xml", FileMode.Open, FileAccess.Read)
        xmldoc.Load(fs)
        xmlnode = xmldoc.GetElementsByTagName("Product")
        For i = 0 To xmlnode.Count - 1
            xmlnode(i).ChildNodes.Item(0).InnerText.Trim()
            str = xmlnode(i).ChildNodes.Item(0).InnerText.Trim() & "  " & xmlnode(i).ChildNodes.Item(1).InnerText.Trim() & "  " & xmlnode(i).ChildNodes.Item(2).InnerText.Trim()
            MsgBox(str)
        Next
    End Sub
End Class
    
answered by 28.04.2018 / 05:34
source