screw you
screw you

Reputation: 141

How do I read this Xml file with my vb 2010 script

so i got my xml file here. the file is called MyName.xml

<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<Data>
  <Person>
    <Name>ggg</Name>
    <Email>erty</Email>
    <Tel>567890-</Tel>
  </Person>
  <Person>
    <Name>hank</Name>
    <Email>3456</Email>
    <Tel>234567890-</Tel>
  </Person>
</Data>

I don't know how to get this function to read this xml file. I feel like I am close to getting this right. I copied the code from the book, then I changed some parts. I just don't know how to get it to change for me to use it. I am also very confused on some stuff here. I left comments on it. But i would like to know what am i missing for the error to keep popping up.

Error - Type 'Person' is not defined. (lines 8, 10, 20)

Also for anyone who has the same book (murach's Visual Basic 2010) the original code before I changed it is on page 713

The original code from the book used a xml file that had 4 entries. Each with 3 parts, a lot like mine.

Imports System.Xml
Imports System.IO

Public Class Form2

    Private Const path As String = "MyName.xml" 'it is in the bin folder.'

    Public Shared Function GetPerson() As List(Of Person) 'what does this do? (Of Person) Am i missing something?'

        Dim people As New List(Of Person) 'Here it is again. What do i do to fix this partpart'

        Dim settings As New XmlReaderSettings
        settings.IgnoreComments = True
        settings.IgnoreWhitespace = True

        Dim xmlIn As XmlReader = XmlReader.Create(path, settings)

        If xmlIn.ReadToDescendant("Person") Then
            Do
                Dim person As New Person
                person.Name = xmlIn("Name")
                xmlIn.ReadStartElement("Person")
                person.Email = xmlIn.ReadElementContentAsString
                person.Tel = xmlIn.ReadElementContentAsString     'to make life simple for testing this, for now.'
                people.Add(person)
            Loop While xmlIn.ReadToNextSibling("Person")
        End If

        xmlIn.Close()
        Return people

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'I will end up puting code here to do something later. Just trying stuff right now.' 


    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub
End Class

Any help would be nice. I know there is a better way and maybe even easier but i need to to pass this class i am taking. So all i got is this code and a really big book. Later for the actual assignment i have to add this to an array in vb and then search it and bla bla bal.... so this is just me understanding the code so i can get to that part. Thanks to any and all that can help me.

Upvotes: 0

Views: 185

Answers (2)

Tim
Tim

Reputation: 28530

Public Shared Function GetPerson() As List(Of Person) 'what does this do? (Of Person) Am i missing something?'

List(Of Person) is declaring a generic list that is of type Person. This will be a collection of Person objects.

The error message "Type Person is not defined" is telling you the your program can't find the definition for Person - most likely because you have not defined a class. You'll need something like this (based on your posted code):

Public Class Person

    Public Property Name As String
    Public Property Email As String
    Public Property Tel As String
End Person 

Upvotes: 1

igrimpe
igrimpe

Reputation: 1785

What you are missing is the type "person" - which the code requires to work. A simple implementation could look like:

Public Class Person
   Public Name As String
   Public Tel As String
   Public EMail As String
End Class

Upvotes: 1

Related Questions