Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I have an xml file on below. I want to select 1 record from this data by checking two condition, for this I write a query in vb.net like this
VB
Dim nod As XmlNode = xd.SelectSingleNode("/Table/Record[TXN_CODE='" & tbxTxnCode.Text & "']")

It works fine but when I try to add the other condition it throws an error saying invalid token xpath exception
ERRROR CODE
VB
Dim enodE As XmlNode = xd.SelectSingleNode("/Table/Record[TXN_CODE='MRV'] AND [DOC_NO = 1002]")

I tried almost every combination like
VB
Dim enodE As XmlNode = xd.SelectSingleNode("/Table/Record[TXN_CODE='MRV'] , [DOC_NO = 1002]") 
Dim enodE As XmlNode = xd.SelectSingleNode("/Table/Record[TXN_CODE='MRV' AND DOC_NO = 1002]") 
Dim enodE As XmlNode = xd.SelectSingleNode("/Table/Record[TXN_CODE='MRV' ,DOC_NO = 1002]")

Anybody have an Idea

MRV
1002
764
QQQ
KRV
1003
868
ASD
Posted
Updated 24-Feb-21 9:58am
v3
Comments
Narayan.mi 8-Nov-11 8:35am    
how to save the selected xml node and save that selected node as another xml file by specifying the file path.

I don't know the XPath without seeing your XML. So I give general links about using XPath with c#

Manipulate XML data with XPath and XmlDocument (C#)[^]

http://support.microsoft.com/kb/308333[^]

http://support.microsoft.com/kb/308343[^]
 
Share this answer
 
Comments
Espen Harlinn 15-Mar-11 15:17pm    
Good links, my 5
Albin Abel 15-Mar-11 15:38pm    
Thanks Espen Harlinn
Sergey Alexandrovich Kryukov 15-Mar-11 21:08pm    
Good source, a 5.
--SA
Albin Abel 16-Mar-11 8:50am    
Thanks SAKryukov
Try this one exactly
"/Table/Record[TXN_CODE='MRV' and DOC_NO = '1002']"

if this doesn't work, remove the quotes for 1002 & check.

If it still does not work, try this. This may not be right
"/Table/Record[TXN_CODE='MRV'] | /Table/Record[DOC_NO = 1002]"

I think this will select all elements with TXN_CODE='MRV' AND DOC_NO=1002
 
Share this answer
 
I happened to concatenate the way you did earlier.

here is the VB version that i used :

VB
singleNode = element.SelectSingleNode("//Item[Slide = '" & appliesTo & "'" & _
                                                         "and ShapeName='" & lazName & "']/Language[@id='" & language & "']")


where Item is a node with Slide and ShapeName as its child nodes.

Here is C# version after converting using this tool http://www.developerfusion.com/tools/convert/vb-to-csharp/[^] is :
C#
singleNode == element.SelectSingleNode("//Item[Slide = '" + appliesTo + "'" + "and ShapeName='" + lazName + "']/Language[@id='" + language + "']")


Hope it helped! :)
 
Share this answer
 
I know this post is very old but it is VERY important that the word "and" is written with a lower case
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900