Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports System.Xml
- Public Class frmMain
- Dim dsPerson As New DataSet("Person")
- Private Sub Setup()
- Dim People As DataTable = dsPerson.Tables.Add("People")
- Dim Name As DataColumn = People.Columns.Add("Name", Type.GetType("System.String"))
- People.Columns.Add("Email", Type.GetType("System.String"))
- People.Columns.Add("Telephone", Type.GetType("System.String"))
- People.Columns.Add("Notes", Type.GetType("System.String"))
- People.PrimaryKey = New DataColumn() {Name}
- dsPerson.AcceptChanges()
- DataGridView1.DataSource = dsPerson
- DataGridView1.DataMember = "People"
- DataGridView1.Refresh()
- End Sub
- Private Sub XMLRead()
- If (cbxXML.Text = "") Then
- MessageBox.Show("No file name entered")
- Else
- If (System.IO.File.Exists(cbxXML.Text.ToString())) Then
- Dim document As XmlReader = New XmlTextReader(cbxXML.Text.ToString())
- Dim PersonRow As DataRow = dsPerson.Tables("People").NewRow()
- While (document.Read())
- Dim type = document.NodeType
- If (type = XmlNodeType.Element) Then
- Debug.Print(document.Name)
- If (document.Name = "Person") Then
- PersonRow = dsPerson.Tables("People").NewRow()
- End If
- If (document.Name = "Name") Then
- Dim x As String = document.ReadInnerXml.ToString()
- Debug.Print(x)
- PersonRow("Name") = x
- End If
- If (document.Name = "Email") Then
- Dim x As String = document.ReadInnerXml.ToString()
- Debug.Print(x)
- PersonRow("Email") = x
- End If
- If (document.Name = "Tel") Then
- Dim x As String = document.ReadInnerXml.ToString()
- Debug.Print(x)
- PersonRow("Telephone") = x
- End If
- If (document.Name = "Notes") Then
- Dim x As String = document.ReadInnerXml.ToString()
- Debug.Print(x)
- PersonRow("Notes") = x
- End If
- End If ' type = XmlNodeType.Element
- If (type = XmlNodeType.EndElement) Then
- Debug.Print("/" & document.Name)
- If (document.Name = "Person") Then
- Debug.Print("*Attempt new row")
- dsPerson.Tables.Item("Person").Rows.Add(PersonRow) 'Error (Object reference
- 'not set to an instance of an object. , NullReferenceException)
- Debug.Print("*New Row Failed")
- End If
- End If
- End While 'document.read()
- dsPerson.AcceptChanges()
- Else
- MessageBox.Show("The filename you selected was not found.")
- End If
- End If
- End Sub
- Private Sub btnLoadXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadXml.Click
- XMLRead()
- Me.DataGridView1.DataSource = dsPerson.Tables("People")
- End Sub
- Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- Setup()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement