Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 9th, 2018, 11:26 AM
Authorized User
Points: 428, Level: 7
Points: 428, Level: 7 Points: 428, Level: 7 Points: 428, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2013
Posts: 94
Thanks: 13
Thanked 0 Times in 0 Posts
Default Expression must evaluate to a node-set.

I have a for each loop that I need help with. I am getting exception "Expression must evaluate to a node-set." when my if statement is executed.
What do I change in the if statement to fix this exception?

Note The objXMLNode can have either CitationDocument or JuvenileCitationDocument. In this particular case, there is only CitationDocument.

objXMLNode object has the following xml

Code:
<CitationDocument xmlns="http://www.courts.state.mn.us/CourtXML/3">
	<Citation>
		<Citee>
			<InCustodyIndicator>true</InCustodyIndicator>
			<IdentificationMethodText>Photo ID</IdentificationMethodText>
		</Citee>
	</Citation>
</CitationDocument>
My for each loop

Code:
For Each objXMLNode In objXMLInputDoc.DocumentElement.SelectNodes("*[(local-name(.)='CitationDocument') or (local-name(.)='JuvenileCitationDocument')]")
    If CBool(objXMLNode.SelectSingleNode("*[(local-name(.)='CitationDocument') or (local-name(.)='JuvenileCitationDocument')]/msc:Citation/msc:Citee/msc:InCustodyIndicator = True", objXMLNameSpaceManager).InnerText) Then
        intPriority = 9
    End If
Next
Reply With Quote
  #2 (permalink)  
Old August 9th, 2018, 11:37 AM
Friend of Wrox
Points: 6,666, Level: 34
Points: 6,666, Level: 34 Points: 6,666, Level: 34 Points: 6,666, Level: 34
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,240
Thanks: 0
Thanked 244 Times in 243 Posts
Default

The methods "SelectNodes" and "SelectSingleNode" only work with XPath expressions that select nodes, if you want to evaluate XPath expressions returning a primitive value like a string, a number or a boolean then in the .NET framework you can do that by first using https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx "CreateNavigator()" on your "XmlNode" (in general) or "XmlDocument" and XPathNavigator has methods like the "Evaluate" method https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx which then returns the XPath result cast to the closest .NET type (e.g.

Code:
Dim value as Boolan = CBool(objXMLNode.CreateNavigator().Evaluate("*[(local-name(.)='CitationDocument') or (local-name(.)='JuvenileCitationDocument')]/msc:Citation/msc:Citee/msc:InCustodyIndicator = 'true'", objXMLNameSpaceManager))
should do to give you a boolean in VB.NET (if the CBool cast is the right way, not really being a VB.NET developer I am not sure about that but the documentation certainly has examples I hope that help you to work it out).
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog

Last edited by Martin Honnen; August 9th, 2018 at 11:41 AM.
Reply With Quote
  #3 (permalink)  
Old August 9th, 2018, 11:49 AM
Authorized User
Points: 428, Level: 7
Points: 428, Level: 7 Points: 428, Level: 7 Points: 428, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2013
Posts: 94
Thanks: 13
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
The methods "SelectNodes" and "SelectSingleNode" only work with XPath expressions that select nodes, if you want to evaluate XPath expressions returning a primitive value like a string, a number or a boolean then in the .NET framework you can do that by first using https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx "CreateNavigator()" on your "XmlNode" (in general) or "XmlDocument" and XPathNavigator has methods like the "Evaluate" method https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx which then returns the XPath result cast to the closest .NET type (e.g.

Code:
Dim value as Boolan = CBool(objXMLNode.CreateNavigator().Evaluate("*[(local-name(.)='CitationDocument') or (local-name(.)='JuvenileCitationDocument')]/msc:Citation/msc:Citee/msc:InCustodyIndicator = 'true'", objXMLNameSpaceManager))
should do to give you a boolean in VB.NET (if the CBool cast is the right way, not really being a VB.NET developer I am not sure about that but the documentation certainly has examples I hope that help you to work it out).
I did try what you suggested. However value is showing False even though it should be true because the element InCustodyIndicator = 'true'
Reply With Quote
  #4 (permalink)  
Old August 9th, 2018, 12:00 PM
Friend of Wrox
Points: 6,666, Level: 34
Points: 6,666, Level: 34 Points: 6,666, Level: 34 Points: 6,666, Level: 34
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,240
Thanks: 0
Thanked 244 Times in 243 Posts
Default

Can you try to simply use
Code:
Dim result As Object = objXMLNode.CreateNavigator().Evaluate("*[(local-name(.)='CitationDocument') or (local-name(.)='JuvenileCitationDocument')]/msc:Citation/msc:Citee/msc:InCustodyIndicator = 'true'", objXMLNameSpaceManager)
and check whether result is as needed in the debugger/IDE?

As for the VB cast, does
Code:
Dim value as Boolan = CType(objXMLNode.CreateNavigator().Evaluate("*[(local-name(.)='CitationDocument') or (local-name(.)='JuvenileCitationDocument')]/msc:Citation/msc:Citee/msc:InCustodyIndicator = 'true'", objXMLNameSpaceManager), Boolean)
give you the wanted result? That is the way used in the docs.

I think the approach should work, haven't tried to write working VB code or checked the details against the XML snippet you have shown. Perhaps https://docs.microsoft.com/en-us/dot...xpathnavigator helps you to find your way around the API or maybe someone else with better VB.NET skills than I have can help with the details for your sample.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
winkimjr2 (August 9th, 2018)
  #5 (permalink)  
Old August 9th, 2018, 12:19 PM
Authorized User
Points: 428, Level: 7
Points: 428, Level: 7 Points: 428, Level: 7 Points: 428, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2013
Posts: 94
Thanks: 13
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
Can you try to simply use
Code:
Dim result As Object = objXMLNode.CreateNavigator().Evaluate("*[(local-name(.)='CitationDocument') or (local-name(.)='JuvenileCitationDocument')]/msc:Citation/msc:Citee/msc:InCustodyIndicator = 'true'", objXMLNameSpaceManager)
and check whether result is as needed in the debugger/IDE?

As for the VB cast, does
Code:
Dim value as Boolan = CType(objXMLNode.CreateNavigator().Evaluate("*[(local-name(.)='CitationDocument') or (local-name(.)='JuvenileCitationDocument')]/msc:Citation/msc:Citee/msc:InCustodyIndicator = 'true'", objXMLNameSpaceManager), Boolean)
give you the wanted result? That is the way used in the docs.

I think the approach should work, haven't tried to write working VB code or checked the details against the XML snippet you have shown. Perhaps https://docs.microsoft.com/en-us/dot...xpathnavigator helps you to find your way around the API or maybe someone else with better VB.NET skills than I have can help with the details for your sample.
I do appreciate your help. I will look around and play with my code until it works.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Presence of a node in a node set EastvanAxon XSLT 4 May 24th, 2013 09:17 AM
XPath: set operation with a disjoint node set rich_unger XSLT 7 May 6th, 2008 10:24 AM
substitute text() node value in XPath expression Coolcampers XSLT 4 April 30th, 2008 09:39 AM
Expression must evaluate to a node-set. XMLUser XSLT 3 February 5th, 2008 01:27 PM
Filtering out from a node set anothervbaddict XSLT 1 May 22nd, 2007 09:42 AM



All times are GMT -4. The time now is 09:38 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.