p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XML (http://p2p.wrox.com/forumdisplay.php?f=91)
-   -   Expression must evaluate to a node-set. (http://p2p.wrox.com/showthread.php?t=99968)

winkimjr2 August 9th, 2018 10:26 AM

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


Martin Honnen August 9th, 2018 10:37 AM

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).

winkimjr2 August 9th, 2018 10:49 AM

Quote:

Originally Posted by Martin Honnen (Post 310225)
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'

Martin Honnen August 9th, 2018 11:00 AM

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.

winkimjr2 August 9th, 2018 11:19 AM

Quote:

Originally Posted by Martin Honnen (Post 310227)
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.


All times are GMT -4. The time now is 07:29 PM.

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