473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

SelectSingleNode fails

I'm trying to get the value of a single node using
SelectSingleNode and I get the NullReference Error.

Example: I want to get the URL in the HTML element but
the GetHTMLPath() function fails on the objNode line.

Any ideas how to make this work? You'll see I've tried
different methods of getting the values. XML 4.0 was so
easy.

I'm using VB.Net 2003 on XP Pro.

Thanks,

King Wilder

===============================================

<?xml version="1.0"?>
<Root xmlns="http://myuri.org/myschema.xsd">
<Settings>
<Paths>
<Images>C:\Stuff\images</Images>
<HTML>C:\Stuff\test2.html</HTML>
<Templates>
<Header>
</Header>
<Footer>
</Footer>
</Templates>
</Paths>
</Settings>
</Root>
'~~~ New Load function.
Public Function LoadThumbs() As XmlDocument
Dim objXML As New XmlDocument
Try
objXML.Load("mysettings.config")
Return objXML
Catch ex As Exception
Return Nothing
Finally
objXML = Nothing
End Try
End Function

'~~~ Get the HTML Path.
Public Function GetHTMLPath() As String
Dim strPath As String
Dim selectExpr As String = String.Empty
Dim myXPathExpr As XPathExpression
Dim objNode As XmlNode
Dim objRoot As XmlNode

Try
'Dim myXPathNavigator As XPathNavigator =
LoadThumbs.CreateNavigator()
'selectExpr = "descendant::HTML"

'myXPathNavigator.MoveToRoot()
'myXPathExpr = myXPathNavigator.Compile
(selectExpr)
''Dim objNode As XmlNode =
myXPathNavigator.Select(myXPathExpr)

'Dim myXPathNodeIterator As XPathNodeIterator
= myXPathNavigator.Select(myXPathExpr)
'MessageBox.Show
(myXPathNodeIterator.Current.ToString)
'strPath = myXPathNodeIterator.Current.Value

Dim objXML As XmlDocument
objXML = LoadThumbs()
MessageBox.Show(objXML.OuterXml)
objRoot = objXML.DocumentElement

'~~~ The following line fails.
objNode = objRoot.SelectSingleNode
("descendant::Settings/Paths/HTML")
MessageBox.Show(objNode.InnerXml)
strPath = objNode.InnerText

'Dim myXmlNodeReader As Xml.XmlNodeReader =
New Xml.XmlNodeReader
(objXML.DocumentElement.SelectSingleNode("//HTML"))
'myXmlNodeReader.Read()
'strPath = myXmlNodeReader.Value
Return strPath
Catch ex As Exception

End Try
End Function

Nov 12 '05 #1
1 4817
Your XPath expression does not account for the
xmlns="http://myuri.org/myschema.xsd" namespace. Take a look at this code
snippet from [0] on MSDN that explains how compose and execute
namespace-aware XPath expressions:

XPathExpression expr = nav.Compile("book/@bk:ISBN");
XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());
mngr.AddNamespace("bk","urn:samples");
expr.SetContext(mngr);
XPathNodeIterator ni = nav.Select(expr);

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0]
http://msdn.microsoft.com/library/en...asp?frame=true

"K. Wilder" <ki**@gizmobeach.com> wrote in message
news:01****************************@phx.gbl...
I'm trying to get the value of a single node using
SelectSingleNode and I get the NullReference Error.

Example: I want to get the URL in the HTML element but
the GetHTMLPath() function fails on the objNode line.

Any ideas how to make this work? You'll see I've tried
different methods of getting the values. XML 4.0 was so
easy.

I'm using VB.Net 2003 on XP Pro.

Thanks,

King Wilder

===============================================

<?xml version="1.0"?>
<Root xmlns="http://myuri.org/myschema.xsd">
<Settings>
<Paths>
<Images>C:\Stuff\images</Images>
<HTML>C:\Stuff\test2.html</HTML>
<Templates>
<Header>
</Header>
<Footer>
</Footer>
</Templates>
</Paths>
</Settings>
</Root>
'~~~ New Load function.
Public Function LoadThumbs() As XmlDocument
Dim objXML As New XmlDocument
Try
objXML.Load("mysettings.config")
Return objXML
Catch ex As Exception
Return Nothing
Finally
objXML = Nothing
End Try
End Function

'~~~ Get the HTML Path.
Public Function GetHTMLPath() As String
Dim strPath As String
Dim selectExpr As String = String.Empty
Dim myXPathExpr As XPathExpression
Dim objNode As XmlNode
Dim objRoot As XmlNode

Try
'Dim myXPathNavigator As XPathNavigator =
LoadThumbs.CreateNavigator()
'selectExpr = "descendant::HTML"

'myXPathNavigator.MoveToRoot()
'myXPathExpr = myXPathNavigator.Compile
(selectExpr)
''Dim objNode As XmlNode =
myXPathNavigator.Select(myXPathExpr)

'Dim myXPathNodeIterator As XPathNodeIterator
= myXPathNavigator.Select(myXPathExpr)
'MessageBox.Show
(myXPathNodeIterator.Current.ToString)
'strPath = myXPathNodeIterator.Current.Value

Dim objXML As XmlDocument
objXML = LoadThumbs()
MessageBox.Show(objXML.OuterXml)
objRoot = objXML.DocumentElement

'~~~ The following line fails.
objNode = objRoot.SelectSingleNode
("descendant::Settings/Paths/HTML")
MessageBox.Show(objNode.InnerXml)
strPath = objNode.InnerText

'Dim myXmlNodeReader As Xml.XmlNodeReader =
New Xml.XmlNodeReader
(objXML.DocumentElement.SelectSingleNode("//HTML"))
'myXmlNodeReader.Read()
'strPath = myXmlNodeReader.Value
Return strPath
Catch ex As Exception

End Try
End Function

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John R. | last post by:
I have an application written in C# and i am using MS XML DOM! I have a document with the following structure (only the <DicEntry> - Elements are important): <NewDataSet...
2
by: Edward Yang | last post by:
My XML document has a default namespace specified by xmlns="some_url". Here it is: <?xml version="1.0" encoding="utf-8" ?> <ssmproject name="sample" server="sql"...
7
by: Jason | last post by:
Hi I have an XML file i need to load and read the contents. Here is the top part of the xml file. <Research xsi:schemaLocation="http://www.rixml.org/2002/6/RIXML...
3
by: muscha | last post by:
Hi All, I have this weird problem. I have an xml document and tried to do an XPath query to it. If I use the SelectSingleNode method it throws an exception but it works with SelectNodes method....
4
by: Rune | last post by:
I have two queries that appear to be exactly the same, but one of them returns null while the other one returns a valid result! Can anyone provide an explanation to why this is so? Below is an...
2
by: adam | last post by:
I tried so many ways to select the node but its not working, please help. I want to research by the identifier in imsmanifest.xml file, the node could be item or resource. XmlDocument doc = new...
7
by: Sashi | last post by:
Two questions: (1) I can pull the text of an XML element as a string just fine using code as such: strSomeString = myXmlDoc.SelectSingleNode("/Element1/Element2/Element3",...
19
by: David Thielen | last post by:
Hi; If there are no namespaces this works fine for me. But if the xml has namespaces, then I get either no node back or an exception. Here is the sample xml: <root xmlns="http://www.test.org"...
0
by: compumate99 | last post by:
I am trying to parse the xml document using selectsinglenode method. I am doing this using Visual Foxpro >>> loResultXml = CreateObject("Microsoft.XMLDOM") With loResultXml .Async = .F. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.