Could not find a part of the path

eddy556

Member
Joined
May 14, 2007
Messages
11
Programming Experience
1-3
Hey I'm using vb.net with xpath in order to access an xml file but I've come across a problem when it comes to loading the xml file into the new XmlDocument(). Here is the code I have:

VB.NET:
Dim document As XmlDocument document = New XmlDocument() 
'Load the XML file into the document object 
document.Load("\customers.xml") 
Dim noCustomers As Integer 
noCustomers = document.SelectNodes("/customers/customer").Count 
MsgBox(noCustomers)

the expcetion being thrown by this is: System.IO.DirectoryNotFoundException: Could not find a part of the path.

The customers.xml is located in the same folder. I have also tried copying it into the Bin folder to no avail.

Many thanks
 
It may be in "same folder", but you're accessing "current folder" when giving relative path, current folder can be anywhere at any given time. Try:
VB.NET:
Dim filepath As String = IO.Path.Combine(Application.StartupPath, "customers.xml")
doc.load(filepath)
 
Doing this I get error:

Error 1 'StartupPath' is not a member of 'System.Windows.Forms.Application'. C:\Documents and Settings\Admin\My Documents\Visual Studio 2005\Projects\Chai Pallets\Chai Pallets\Form1.vb

where you've got Application.StartupPath
 
That is very strange, StartupPath is a property of Application class in all Framework versions (1.0 - 3.5).
About the problem to load the Xml file, have you tried to hardcode the absolute path also? For example "C:\customers.xml"
 
Hi, yes I have hardcoded it, swapped the slashes \ / tried different folders etc etc. Here I have posted the whole class, I've no idea whether it'll help or not. Maybe I'm missing something as the StartupPath won't work?

VB.NET:
Imports System
Imports System.Xml
Imports System.Xml.XPath

Public Class Form1


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Dim selection As Integer

        selection = ComboBox1.SelectedIndex()

        Select Case selection

            Case 0
                lblAction.Visible = False
                rdbtnRepair.Visible = False
                rdbtnChippings.Visible = False

            Case 1
                lblAction.Visible = True
                rdbtnRepair.Visible = True
                rdbtnChippings.Visible = True

        End Select

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ReadXMLFile()
    End Sub

    Private Sub ReadXMLFile()

        Dim document As XmlDocument
        Dim visited As Boolean
        Dim firstName, lastName As String
        Dim customerID As Integer
        'Dim filepath As String = IO.Path.Combine(Application.StartupPath, "customers.xml")

        Try
            document = New XmlDocument()

            'Load the XML file into the document object 
            'THIS IS THE PROBLEM LINE
            document.Load("\customers.xml")

            Dim index
            Dim noCustomers As Integer

            ' noCustomers =  document.SelectNodes("/customers/customer").Count
            MsgBox(document.SelectNodes("/customers/customer").Count)


            'Loop through the nodes
            For index = 1 To noCustomers

                'Get the visited attribute value
                If Convert.ToString(document.SelectSingleNode("//customer[" & index & "]/@visited").InnerText) = "yes" Then
                    visited = True
                Else
                    visited = False
                End If

                'Get the id Element Value
                customerID = Convert.ToInt32(document.SelectSingleNode("//customer[" & index & "]/id").InnerText)

                'Get the firstName Element Value
                firstName = Convert.ToString(document.SelectSingleNode("//customer[" & index & "]/name/firstname").InnerText)

                'Get the lastName Element Value
                lastName = Convert.ToString(document.SelectSingleNode("//customer[" & index & "]/name/lastname").InnerText)

                'create a Customer object
                ' aCustomer = New Customer(firstName, lastName, customerID)
                MsgBox(firstName + lastName)
                'add that Customer object to the list of Customers
                'customerList.Add(aCustomer)
            Next

            'Dim item As ListViewItem

            ' custList.Columns.Add("ID", -2, HorizontalAlignment.Center)
            ' custList.Columns.Add("First Name", -2, HorizontalAlignment.Left)
            ' custList.Columns.Add("Last Name", -2, HorizontalAlignment.Left)

            ' For index = 1 To noCustomers
            'aCustomer = customerList.Item(index - 1)

            ' item = New ListViewItem(aCustomer.getCustomerID())
            ' item.SubItems.Add(aCustomer.getFirstName())
            'item.SubItems.Add(aCustomer.getLastName())

            ' custList.Items.Add(item)
            ' Next

            ' custList.View = View.Details

        Catch errorVariable As Exception
            'Error trapping
            TextBox2.Text = errorVariable.ToString()
        End Try

    End Sub
End Class

I'm sorry that there are loads of comments in there that may confuse - this is my way of just trying different possibilities in the hope it works!

Many thanks
 
yes I have hardcoded it
No, that is not what you have done, this is a relative path pointing to current directory, which can be any path at any given time.
'THIS IS THE PROBLEM LINE
document.Load("\customers.xml")
have you tried to hardcode the absolute path also? For example "C:\customers.xml"
This is just to get the file load sorted of course, at some point you need to get a way of loading the file dynamically sorted. Are you by any chance a Compact Framework consumer?
 
Yes this is just the final version. Here are a few of the filepaths I have tried (including the above):

C:\Documents and Settings\Admin\My Documents\Visual Studio 2005\Projects\Chai Pallets\Chai Pallets\bin\Debug\customers.xml

I have also tried the same with the slashes reversed etc and different folders.

Now I may have discovered something which I forgot to say previously. On the FileNotFound exception when I have a full path (starting with C:\) it puts an extra \ before everything so it ends up as \C:\...and so on. This is why I thought that it would only accept a relative path.

I don't believe I'm using the compact framework. I have just checked the installed versions using the command line and I get:

v1.1.4322
v2.0.50727

Much appreciated
 
Back
Top