[quoted text, click to view] "Victor Hadianto" <synop@nospam.nospam> wrote in message news:O0BPtbbpEHA.596@TK2MSFTNGP11.phx.gbl...
> "flycast" <flycast@discussions.microsoft.com> wrote in message news:BA3BB3E4-3617-4DD6-8EB6-D3D2FA7576B3@microsoft.com...
> > Dim node As XmlNode = xmlDocument.SelectSingleNode("/Parameters")
> > RobotName.Text = node.SelectSingleNode("Name").InnerText
> >
> > I get the dreaded "Object reference not set to an instance of an object."
Check whether node is Nothing, flycast, and you're less likely to feel dreadful.
If ( node Is Nothing ) Then
' Do Something Else.
Else
RobotName.Text = node.SelectSingleNode( "Name").InnerText
End If
I'll point out that the Else block can still produce a NullReferenceException,
if the Name node is not found (see next point).
: :
[quoted text, click to view] > > <Robot xmlns="
http://tempuri.org/RobotDefaults.xsd"> > > <Parameters>
: :
> Try //Robot/Parameters
Also try using the XmlNamespaceManager, adding the default namespace URI
of "
http://tempuri.org/RobotDefaults.xsd" to it, and passing that along to Select-
SingleNode( ), like this,
Dim nsMan As XmlNamespaceManager = New XmlNamespaceManager( xmlDocument.NameTable)
nsMan.AddNamespace( "def", "
http://tempuri.org/RobotDefaults.xsd")
Dim node As XmlNode = xmlDocument.SelectSingleNode( "/def:Robot/def:Parameters", nsMan)
[quoted text, click to view] > > Can anybody show me how to retrieve the "x" number Parameter node from this
> > document?
XPath lets you use position( ) = x in a predicate (which can be abbreviated to [x] almost like
a C/C++/C# array index). However in XPath, positions start numbering at 1 (so you can
retrieve the 1st Parameter, the 2nd Parameter, etc.). Ammend the previous code snippet
as follows to fetch the second parameter node,
' Code as before, now retrieving the second Parameter node of Robot.
Dim paramNumber As Integer = 2
Dim node As XmlNode = xmlDocument.SelectNodes( "/def:Robot/def:Parameters[" + CStr( paramNumber) + "]", nsMan)
Derek Harmon