?
Solved

Read XML File VB.Net

Posted on 2008-02-12
5
Medium Priority
?
477 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:martgriff
  • 3
  • 2
5 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 20877661

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
 

Author Comment

by:martgriff
ID: 20877763
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
 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 20877907
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 20877975
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
 

Author Closing Comment

by:martgriff
ID: 31430279
Great Help. Thank you very Much.................
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question