Connecting Tech Pros Worldwide Forums | Help | Site Map

SelectSingleNode problem in vb .net

Newbie
 
Join Date: Jul 2008
Posts: 2
#1: Jul 3 '08
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
Expand|Select|Wrap|Line Numbers
  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>
  9.  
Here is the code:
Expand|Select|Wrap|Line Numbers
  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
  23.  
  24.  
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!

insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#2: Jul 3 '08

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
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#3: Jul 3 '08

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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Me.txtImportPath.Text = xSettings.SelectSingleNode("//root/settings/importdir").InnerText
  3. Me.txtExportPath.Text = xSettings.SelectSingleNode("//root/settings/importdir").InnerText
  4.  
Does that help at all?

Dr B
Newbie
 
Join Date: Jul 2008
Posts: 2
#4: Jul 3 '08

re: SelectSingleNode problem in vb .net


Nevermind, just figured it out. It was loading the wrong XML file!
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#5: Jul 3 '08

re: SelectSingleNode problem in vb .net


Glad you found the answer.
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#6: Jul 4 '08

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