SelectSingleNode problem in vb .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bharnett
    New Member
    • Jul 2008
    • 2

    SelectSingleNode problem in vb .net

    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:
    <?xml version="1.0" encoding="utf-8" ?>
    <root>
      <settings>
        <importdir>test</importdir>
        <exportdir>test</exportdir>
        <archivedir>test</archivedir>
      </settings>  
    </root>
    Here is the code:
    Code:
    Public Class Form1
        Dim xSettings As New System.Xml.XmlDocument
        Dim xSettingsFileName As String = String.Empty
        Dim sFilePath As String
    
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            xSettingsFileName = "loaderconfig.XML"
            sFilePath = My.Application.Info.DirectoryPath & "\" & xSettingsFileName
            'sFilePath = "C:\My Documents\Visual Studio 2005\Projects\OTPP Loader\OTPP Loader\bin\Debug\loaderconfig.xml"
            refreshconfig()
    
        End Sub
    
        Public Sub refreshconfig()
            'Clear out the text boxes to ensure nothing is there when we load the settings
            Me.txtImportPath.Text = String.Empty
            Me.txtExportPath.Text = String.Empty
    
            xSettings.Load(sFilePath)
    
            Me.txtImportPath.Text = xSettings.SelectSingleNode("root/settings/importdir").InnerText
            Me.txtExportPath.Text = xSettings.SelectSingleNode("root/settings/importdir").InnerText
    It is erroring on Me.txtImportPat h.Text = xSettings.Selec tSingleNode("ro ot/settings/importdir").Inn erText 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 Curtis Rutland; Jul 3 '08, 04:11 PM. Reason: Added Code Tags - Please use the # button
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    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

    Comment

    • DrBunchman
      Recognized Expert Contributor
      • Jan 2008
      • 979

      #3
      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:
         
      Me.txtImportPath.Text = xSettings.SelectSingleNode("//root/settings/importdir").InnerText
      Me.txtExportPath.Text = xSettings.SelectSingleNode("//root/settings/importdir").InnerText
      Does that help at all?

      Dr B

      Comment

      • bharnett
        New Member
        • Jul 2008
        • 2

        #4
        Nevermind, just figured it out. It was loading the wrong XML file!

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Glad you found the answer.

          Comment

          • DrBunchman
            Recognized Expert Contributor
            • Jan 2008
            • 979

            #6
            Originally posted by bharnett
            Nevermind, just figured it out. It was loading the wrong XML file!
            Ah, well that would help! :-)

            Dr B

            Comment

            Working...