Reading an XML file

mtaylor314

Member
Joined
Nov 30, 2006
Messages
19
Location
Cleves, OH
Programming Experience
Beginner
I need some help authenticating through an XML file. The .XML has the following format
HTML:
<USERS>
     <user1>pw</user1>
     <user2>pw</user2>
     ...
</USERS>
I use this code to read and authenticate
VB.NET:
    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As _
                                      System.Web.UI.WebControls.AuthenticateEventArgs) _
                                      Handles Login1.Authenticate
        'Using xTR As XmlTextReader = New XmlTextReader(Server.MapPath("Data Files\UserData.xml"))

        Dim strUser As String = FTPSite & "UserData.xml"
        Dim settings As New XmlReaderSettings()
        settings.IgnoreComments = True
        settings.IgnoreWhitespace = True
        settings.IgnoreComments = True
        Dim reader As XmlReader = XmlReader.Create(strUser, settings)
        Using reader
            Try
                reader.ReadStartElement("USERS")
                Try
                    reader.ReadStartElement(Login1.UserName.ToUpper)
                    If reader.Value.CompareTo(Login1.Password) = 0 Then
                        e.Authenticated = True
                        Exit Sub
                    End If
                    Login1.FailureText = "Invalid Password"
                Catch ex As Exception
                    Login1.FailureText = "Invalid User Name"
                End Try

            Catch ex As Exception
                Login1.FailureText = "Unable to read UserData file. Err: " & ex.Message
            End Try
        End Using
    End Sub
This only allows me to login in the first user, any other user apparently does not exist. Any pointers would be appreciated

I then tried this code as authentication, but it does not allow any user to log in. It will go through the For/Next loop once and the value of node.tostring is "System.Xml.XmlElement"
VB.NET:
Try
            Dim oXmlDocument As New XmlDocument
            Dim NodeList As XmlNodeList

            oXmlDocument.Load(FTPSite & "UserData.xml")
            NodeList = oXmlDocument.SelectNodes("/USERS")

            For Each Node As XmlNode In NodeList
                If Node.ToString.ToUpper = Login1.UserName.ToUpper Then
                    Try
                        If Node.InnerText.CompareTo(Login1.Password) = 0 Then
                            e.Authenticated = True
                            Exit Sub
                        End If
                        Login1.FailureText = "Invalid Password"
                    Catch ex As Exception
                        Login1.FailureText = "Invalid User Name"
                    End Try
                End If
            Next

        Catch ex As Exception
            Login1.FailureText = "Unable to read UserData file.  Err: " & ex.Message
        End Try
 
Last edited:
I have figured it out, here is the code that I used:
VB.NET:
        Try
            Dim oXmlDocument As New XmlDocument
            Dim NodeList As XmlNodeList

            oXmlDocument.Load(FTPSite & "UserData.xml")
            NodeList = oXmlDocument.SelectNodes("/USERS")

            For Each Node As XmlNode In NodeList
                For i As Integer = 0 To Node.ChildNodes.Count - 1
                    Dim username As String = Node.ChildNodes.Item(i).Name
                    If username.ToUpper = Login1.UserName.ToUpper Then
                        Try
                            Dim password As String = Node.ChildNodes.Item(i).InnerText
                            If password.CompareTo(Login1.Password) = 0 Then
                                e.Authenticated = True
                                Exit Sub
                            End If
                            Login1.FailureText = "Invalid Password"
                        Catch ex As Exception
                            Login1.FailureText = "Invalid User Name"
                        End Try
                    End If
                Next
            Next

        Catch ex As Exception
            Login1.FailureText = "Unable to read UserData file.  Err: " & ex.Message
        End Try
    End Sub
 
Thank you for the suggestion. I'll work on that and the first time I tried it, it did not error out correctly because when I use the username in the xpath, and it is not there, it still goes through and does not error out as "user not found" error. It does work when the user is found, however

VB.NET:
Try
            Dim oXmlDocument As New XmlDocument
            Dim NodeList As XmlNodeList

            oXmlDocument.Load(FTPSite & "UserData.xml")
            Try
                NodeList = oXmlDocument.SelectNodes("/USERS/" & Login1.UserName.ToUpper)
                For Each Node As XmlNode In NodeList
                    Dim password As String = Node.InnerText
                    If password.CompareTo(Login1.Password) = 0 Then
                        e.Authenticated = True
                        Exit Sub
                    End If
                    Login1.FailureText = "Invalid Password"
                Next
            Catch ex As Exception
                Login1.FailureText = "Invalid User Name"
            End Try
        Catch ex As Exception
            Login1.FailureText = "Unable to read UserData file.  Err: " & ex.Message
        End Try
 
Back
Top