Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im working on this function that will read a xml file for a specific entry and if its there it will write its information to a text file. in the long run I m will be changing the names to computer names and ip address that will get added to the host file, but for know to not disclose any network information im using email and user names. I can get it to read the xml file but when it writes to a text file its output is :
VB
System.Xml.XmlElement
System.Xml.XmlElement


if someone can look at my code and tell me what i'm doing wrong that would be great. below is my xml file called Contact.xml.

XML
<?xml version="1.0" encoding="utf-8" ?>
 <CAE>
         <base>
                 <caedata>John</caedata>
         <caepolicy>Smith</caepolicy>
         <basecode>john.smith@xyzmail.com</basecode>
         </base>
         <base>
                 <caedata>Suzan</caedata>
                 <caepolicy>Carter</caepolicy>
                 <basecode>suzan.carter@xyzmail.com</basecode>
         </base>
         <base>
                 <caedata>Tom</caedata>
                 <caepolicy>Baker</caepolicy>
                 <basecode>tom.baker@xyzmail.com</basecode>
         </base>
 </CAE>


here is my function I created and the button click that I have that pulls the information I put into the textbox.
VB
Public Class Form1

    Dim xmlDoc As New XmlDocument()

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

        If redirect(TextBox1.Text) = True Then
            MsgBox("completed")
        Else
            MsgBox("Failed to complete")
        End If

    End Sub

    Function redirect(ByVal BASE As String)
        Dim fileLoc As String = "C:\Users\Zack\Desktop\hostfile.txt"
        Dim CP
        Dim CD
        xmlDoc.Load("C:\Users\Zack\Desktop\Contact.xml")
        Dim xmlContactNodeList As XmlNodeList = xmlDoc.GetElementsByTagName("base")

        For i = 0 To xmlContactNodeList.Count - 1
            If xmlContactNodeList.Item(i).Item("basecode").InnerText = BASE Then
                CP = xmlContactNodeList.Item(i).Item("caepolicy").ToString
                CD = xmlContactNodeList.Item(i).Item("caedata").ToString
                If File.Exists(fileLoc) Then
                    Using w As New System.IO.StreamWriter(fileLoc, True)
                        w.WriteLine(CD & vbCrLf & CP)
                    End Using
                    Return True
                    Exit Function
                End If
            End If
        Next

        Return False
    End Function
End Class
Posted

1 solution

I figured out what I did wrong. when I m declaring cp and cd im using .tostring when I should have been using innertext. once I changed it everything worked.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900