Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

SelectSingleNode problem in vb .net

Question posted by: bharnett (Newbie) on July 3rd, 2008 04:07 PM
I pulled this code from a different application we use to write/read data between an xml and a form. The information I am trying to read is just a string of a directory path that the user picks on the form.

Here is the XML file
Code: ( text )
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <root>
  3.   <settings>
  4.     <importdir>test</importdir>
  5.     <exportdir>test</exportdir>
  6.     <archivedir>test</archivedir>
  7.   </settings> 
  8. </root>


Here is the code:
Code: ( text )
  1. Public Class Form1
  2.     Dim xSettings As New System.Xml.XmlDocument
  3.     Dim xSettingsFileName As String = String.Empty
  4.     Dim sFilePath As String
  5.  
  6.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7.         xSettingsFileName = "loaderconfig.XML"
  8.         sFilePath = My.Application.Info.DirectoryPath & "\" & xSettingsFileName
  9.         'sFilePath = "C:\My Documents\Visual Studio 2005\Projects\OTPP Loader\OTPP Loader\bin\Debug\loaderconfig.xml"
  10.         refreshconfig()
  11.  
  12.     End Sub
  13.  
  14.     Public Sub refreshconfig()
  15.         'Clear out the text boxes to ensure nothing is there when we load the settings
  16.         Me.txtImportPath.Text = String.Empty
  17.         Me.txtExportPath.Text = String.Empty
  18.  
  19.         xSettings.Load(sFilePath)
  20.  
  21.         Me.txtImportPath.Text = xSettings.SelectSingleNode("root/settings/importdir").InnerText
  22.         Me.txtExportPath.Text = xSettings.SelectSingleNode("root/settings/importdir").InnerText

It is erroring on Me.txtImportPath.Text = xSettings.SelectSingleNode("root/settings/importdir").InnerText and the error I'm getting is:
Object reference not set to an instance of an object.

I don't know why this is happening as it works great in the application I grabbed this code from.

THANKS!
Last edited by insertAlias : July 3rd, 2008 at 04:11 PM. Reason: Added Code Tags - Please use the # button
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
insertAlias's Avatar
insertAlias
Moderator
1,100 Posts
July 3rd, 2008
04:11 PM
#2

Re: SelectSingleNode problem in vb .net
Welcome to Bytes!

We appreciate it if you wrap your code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. And if it's easier for us to read, it's easier for us to help.

You can type them in or use the # button on the text editor.

MODERATOR

Reply
DrBunchman's Avatar
DrBunchman
Moderator
763 Posts
July 3rd, 2008
04:32 PM
#3

Re: SelectSingleNode problem in vb .net
Hi bharnett,

This error means that the XPath query is not evalutating to a node correctly i.e. the XPath is probably wrong.

First thing to do is test that XPath string - have you got Stylus Studio or a similar XML editor which you can use to test these queries? That will make your life much easier and I think there are some free ones out there.

Can you try putting the two forward slashes at the beginning of all of your XPath strings and see if that makes any difference. e.g.
Code: ( text )
  1. Me.txtImportPath.Text = xSettings.SelectSingleNode("//root/settings/importdir").InnerText
  2. Me.txtExportPath.Text = xSettings.SelectSingleNode("//root/settings/importdir").InnerText

Does that help at all?

Dr B

Reply
bharnett's Avatar
bharnett
Newbie
2 Posts
July 3rd, 2008
04:48 PM
#4

Re: SelectSingleNode problem in vb .net
Nevermind, just figured it out. It was loading the wrong XML file!

Reply
insertAlias's Avatar
insertAlias
Moderator
1,100 Posts
July 3rd, 2008
05:49 PM
#5

Re: SelectSingleNode problem in vb .net
Glad you found the answer.

Reply
DrBunchman's Avatar
DrBunchman
Moderator
763 Posts
July 4th, 2008
06:43 AM
#6

Re: SelectSingleNode problem in vb .net
Quote:
Originally Posted by bharnett
Nevermind, just figured it out. It was loading the wrong XML file!

Ah, well that would help! :-)

Dr B

Reply
Reply
Not the answer you were looking for? Post your question . . .
183,630 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top .NET Forum Contributors