XmlDocument.Load() problems

eddy556

Member
Joined
May 14, 2007
Messages
11
Programming Experience
1-3
Hi,

I have a PDA application which reads customer details from an XML file called customers.xml. This works fine and I have complete programatical access to the file.

However I wish to adapt it to load another XML file instead, named trentassetdatadocument.xml. However upon doing this I get an error "System.IO.FileNotFoundException: Could not find file '\Program Files\XMLReaderTest\trentassetdatadocument.xml'

The funny thing is, both files are stored in the bin\Debug file of the application. However neither of the URLs of the files are \Program Files\XMLReaderTest\ even though one works. Thinking I had a duplicate file (stored at \Program Files\XMLReaderTest\) I changed some of the data, however on doing this the changes are reflected in the app.

I'm at at a complete loss here, any ideas whats going on?

I have placed both XML files here:

customers.xml
VB.NET:
<?xml version="1.0" standalone="yes" ?>
<customers>
  <customer visited="no">
    <id>506</id>
    <name>
      <firstname>Reg</firstname>
      <lastname>Kemp</lastname>
    </name>
  </customer>
  <customer visited="yes">
    <id>329</id>
    <name>
      <firstname>Kerry arse</firstname>
      <lastname>Gorblimey</lastname>
    </name>
  </customer>
</customers>

trentassetdatadocument.xml
VB.NET:
<?xml version="1.0" standalone="yes"?>
<Data>
	<EPCData>
		<EPC1>0</EPC1>
		<EPC2>00000</EPC2>
		<EPC3>0007</EPC3>
		<EPC4>7DC24E</EPC4>
	</EPCData>
	<EPCData>
		<EPC1>0</EPC1>
		<EPC2>00500</EPC2>
		<EPC3>0000</EPC3>
		<EPC4>121743</EPC4>
	</EPCData>
</Data>

The error occurs on load. The line is XmlDocument.Load("\Program Files\XMLReaderTest\(whichever file).xml")
 
Program Files?

Are you trying to load a local file on your system? If so you'll want to have a drive letter in the load path.
 
If they are stored alongside your app, dont use any folder names in the path

I suggest that you install and use ProcMon from sysinternals. It will tell you, when the .vshost.exe requests to load the file, what file it is attempting to load, where from and whether it failed or not. Very useful utility
 
Okay, when I add the drive letter first. The file it tries to load is \C:\Program Files\XMLReaderTest\ i.e. it appears to be adding the \ slash whenever the URL I supply doesn't have one at the start.

I will look into installing ProcMon, thanks
 
I personally prefer to use datasets as well.

How are you setting up your XmlDocument.

VB.NET:
Dim doc As XmlDocument = New XmlDocument()
        doc.Load("C:\Test\TestXML.xml")

I tried this and it didn't try to prepend a slash the file.
 
Okay I'll post you the full sub including the imports at the top

VB.NET:
Private Sub ReadXMLFile()
        Dim aCustomer As Customer
        Dim visited As Boolean
        Dim firstName, lastName As String
        Dim customerID As Integer

        Try
            document = New XmlDocument()

            'Load the XML file into the document object
            document.Load("C:\Program Files\XMLReaderTest\trentassetdatadocument.xml")
            ' document.Load("\Program Files\XMLReaderTest\customers.xml")
            MsgBox("worked")
            'Dim index, noCustomers As Integer

            ' noCustomers = 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)

            '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
            MsgBox(errorVariable.ToString())
        End Try
    End Sub

I realise there are lots of comments, they are there from when the file worked.

At the top are the lines:

Imports System
Imports System.Xml
Imports System.Xml.XPath
 

Latest posts

Back
Top