Reading from an XML document

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ericstein81
    New Member
    • Jul 2007
    • 14

    Reading from an XML document

    Hey all,

    I have a program that is saving information into and XML file that is later read from to continue the users progress. I am having trouble getting the reader to work without an error

    This is the code that I am having trouble with and below is the error. Thanks for the help!


    Code:
    Dim xmldoc As XmlDataDocument = New XmlDataDocument
     
    xmldoc.Load("please.xml")
     
    [B]TextBox1.Text = xmldoc.SelectSingleNode("form1/text").InnerText[/B]
    TextBox1.BackColor = System.Drawing.ColorTranslator.FromHtml(xmldoc.SelectSingleNode("form1/color").InnerText)
    TextBox2.Text = xmldoc.SelectSingleNode("form1/text").InnerText
    TextBox2.BackColor = System.Drawing.ColorTranslator.FromHtml(xmldoc.SelectSingleNode("form1/color").InnerText)



    System.NullRefe renceException was unhandled
    Message="Object reference not set to an instance of an object."
    Source="Windows Application2"
    StackTrace:
    at WindowsApplicat ion2.Form1.Butt on2_Click(Objec t sender, EventArgs e) in d:\Users\steinm etze\Documents\ Visual Studio 2008\Projects\W indowsApplicati on2\WindowsAppl ication2\Form1. vb:line 100
    at System.Windows. Forms.Control.O nClick(EventArg s e)
    at System.Windows. Forms.Button.On Click(EventArgs e)
    at System.Windows. Forms.Button.On MouseUp(MouseEv entArgs mevent)
    at System.Windows. Forms.Control.W mMouseUp(Messag e& m, MouseButtons button, Int32 clicks)
    at System.Windows. Forms.Control.W ndProc(Message& m)
    at System.Windows. Forms.ButtonBas e.WndProc(Messa ge& m)
    at System.Windows. Forms.Button.Wn dProc(Message& m)
    at System.Windows. Forms.Control.C ontrolNativeWin dow.OnMessage(M essage& m)
    at System.Windows. Forms.Control.C ontrolNativeWin dow.WndProc(Mes sage& m)
    at System.Windows. Forms.NativeWin dow.DebuggableC allback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows. Forms.UnsafeNat iveMethods.Disp atchMessageW(MS G& msg)
    at System.Windows. Forms.Applicati on.ComponentMan ager.System.Win dows.Forms.Unsa feNativeMethods .IMsoComponentM anager.FPushMes sageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo pInner(Int32 reason, ApplicationCont ext context)
    at System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo p(Int32 reason, ApplicationCont ext context)
    at System.Windows. Forms.Applicati on.Run(Applicat ionContext context)
    at Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase.OnRun ()
    at Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase.DoApp licationModel()
    at Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase.Run(S tring[] commandLine)
    at WindowsApplicat ion2.My.MyAppli cation.Main(Str ing[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.v b:line 81
    at System.AppDomai n._nExecuteAsse mbly(Assembly assembly, String[] args)
    at System.AppDomai n.ExecuteAssemb ly(String assemblyFile, Evidence assemblySecurit y, String[] args)
    at Microsoft.Visua lStudio.Hosting Process.HostPro c.RunUsersAssem bly()
    at System.Threadin g.ThreadHelper. ThreadStart_Con text(Object state)
    at System.Threadin g.ExecutionCont ext.Run(Executi onContext executionContex t, ContextCallback callback, Object state)
    at System.Threadin g.ThreadHelper. ThreadStart()
    InnerException:
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    SelectSingleNod e(xpath) returns null if xpath expression is not found. Are you sure that "form1/text" is a valid xpath expression for your schema?

    Check this link for some clues: Select Nodes Using XPath Navigation

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      I would also advise having a try-catch around your code if you are going to be calling functions on nodes before checking that they are null.
      If you post part of your xml, we might also be able to help.

      Comment

      Working...