Read XML File VB.Net

I have an XML File Called "settings.xml" within the file i have the following:
<?xml version="1.0" encoding="utf-8" ?>
<ServerDetails>
  <PilotFolder>\\Worcester\Pilot\</PilotFolder>
</ServerDetails>
How do i read the data in the XML File (PilotFolder) and add it to a string in my app that i can call?
My app is VB.Net
martgriffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulHewsCommented:

Imports System.Xml
Public Class Form1
    
 
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xmlDoc As New XmlDocument
        xmlDoc.Load("C:\temp\test.xml")  ' Can also point to an URL.  Load an XML String with LoadXML method
        Dim node As XmlNode = xmlDoc.SelectSingleNode("ServerDetails/PilotFolder")
        MsgBox(node.InnerText)
 
    End Sub
End Class

Open in new window

0
martgriffAuthor Commented:
Sorry Im new to coding, Im not 100% sure where i need to put that bit of code. The XML file will be in the root folder with the .exe so do i need to enter the location? Would you be kind and show me where that code needs to go I want to  "    Private Const ROOTFOLDER As String =  " to = the PilotFolder from the XML.............
Imports System
Imports System.IO
Imports System.Xml
 
 
Class Form1
 
    Private Delegate Function CopyProgressRoutine(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
    Private Declare Auto Function CopyFileEx Lib "kernel32.dll" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal lpProgressRoutine As CopyProgressRoutine, ByVal lpData As Int32, ByVal lpBool As Int32, ByVal dwCopyFlags As Int32) As Int32
    Private _totalFileSize As Long = 0
    Private _totalBytesCopied As Long = 0
    Private _copyProgressRoutine As CopyProgressRoutine
    Private Const ROOTFOLDER As String = 
    Private Const DESTFOLDER As String = "D:\PilotNew\"
    Private Const FileName As String = "D:\PilotNew\Pilot_2007.adp"
    Private Function CopyProgress(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
        ProgressBar1.Value = Convert.ToInt32((_totalBytesCopied + totalBytesTransferred) / _totalFileSize * 100)
        Application.DoEvents()
    End Function
    Private Sub GetTotalFileSize(ByVal folder As System.IO.DirectoryInfo)
        For Each fi As System.IO.FileInfo In folder.GetFiles
            _totalFileSize += fi.Length
        Next
        For Each di As System.IO.DirectoryInfo In folder.GetDirectories
            GetTotalFileSize(di)
        Next
    End Sub
 
    Private Sub CopyFiles(ByVal folder As System.IO.DirectoryInfo, ByVal destinationFolder As String)
        If Not System.IO.Directory.Exists(destinationFolder) Then
            System.IO.Directory.CreateDirectory(destinationFolder)
        End If
        For Each fi As System.IO.FileInfo In folder.GetFiles
            CopyFileEx(fi.FullName, destinationFolder & "\" & fi.Name, _copyProgressRoutine, 0, 0, 0)
            _totalBytesCopied += fi.Length
        Next
        For Each di As System.IO.DirectoryInfo In folder.GetDirectories
            CopyFiles(di, di.FullName.Replace(ROOTFOLDER, DESTFOLDER))
        Next
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
    End Sub
 
    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Application.ExitThread()
 
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Please Wait.........."
        Label1.ForeColor = Color.Red
        DeleteFile(FileName)
        GetTotalFileSize(New System.IO.DirectoryInfo(ROOTFOLDER))
        _copyProgressRoutine = New CopyProgressRoutine(AddressOf CopyProgress)
        CopyFiles(New System.IO.DirectoryInfo(ROOTFOLDER), DESTFOLDER)
        Label1.Text = "Pilot Upgraded Sucessfully"
        Label1.ForeColor = Color.Red
    End Sub
    Sub DeleteFile(ByVal FileName As String)
        If IO.File.Exists(FileName) Then IO.File.Delete(FileName)
    End Sub
End Class

Open in new window

0
PaulHewsCommented:
First, change the declaration, as you can't change the value of a Const:

Private RootFolder As String = ""

Now in the form_load event:


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim xmlDoc As New XmlDocument
    xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory() & "Settings.xml")
    Dim node As XmlNode = xmlDoc.SelectSingleNode("ServerDetails/PilotFolder")
    RootFolder = node.InnerText
End Sub
   
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulHewsCommented:
If you have VS 2005, you can use the my.settings classes to achieve a similar result much easier.

http://msdn2.microsoft.com/en-us/library/a65txexh.aspx

If you have 2003, you can use AppSettingsReader, which admittedly is not as nice as the My.Settings objects:

http://msdn2.microsoft.com/en-us/library/system.configuration.appsettingsreader.aspx
0
martgriffAuthor Commented:
Great Help. Thank you very Much.................
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.