all groups > dotnet xml > march 2004 >
You're in the

dotnet xml

group:

XPath Query Problem


RE: XPath Query Problem tMan
3/31/2004 11:21:11 AM
dotnet xml:
[quoted text, click to view]

enclose sID with single quote in the xpath expression. result should be like...[id='GUID-758t-ETC'

("//location[id='" & sID &"']"


----- Kyle Novak wrote: ----

I am having a problem with an XPath expression not finding a node in certai
instances. My function DelLocationByID takes an ID of the element to delet
from an XML file. This code works for numbers such as 3423423 or 89709023
but it does not work for something like e1156c58-f9fd-44aa-8f15-d6e7930bc52
(which happens to be a GUID). The sample XML document is displayed belo
with an element that the SelectSingleNode function does not find (but work
for say 2323403)

So my question is, why does the XPath expression not find the GUID but doe
find a regular number? (I'm assuming the '-' characters have something t
do with it. Do I have to escape the '-' chars or something

Thanks

Kyl


---------------------------------------------------------------------------
--------------------------
Public Function DelLocationByID(ByVal sID As String

moDoc.Load(msFilePath

Dim oNode As XmlNode = moDoc.SelectSingleNode("//location[id=" & sID
"]"
moDoc.DocumentElement.RemoveChild(oNode) <--oNode IS NULL AFTE
SEARCHING BY GUI
moDoc.Save(msFilePath

End Functio


XPath Query Problem Kyle Novak
3/31/2004 12:15:32 PM
I am having a problem with an XPath expression not finding a node in certain
instances. My function DelLocationByID takes an ID of the element to delete
from an XML file. This code works for numbers such as 3423423 or 89709023,
but it does not work for something like e1156c58-f9fd-44aa-8f15-d6e7930bc524
(which happens to be a GUID). The sample XML document is displayed below
with an element that the SelectSingleNode function does not find (but works
for say 2323403).

So my question is, why does the XPath expression not find the GUID but does
find a regular number? (I'm assuming the '-' characters have something to
do with it. Do I have to escape the '-' chars or something?

Thanks,

Kyle


----------------------------------------------------------------------------
---------------------------
Public Function DelLocationByID(ByVal sID As String)

moDoc.Load(msFilePath)

Dim oNode As XmlNode = moDoc.SelectSingleNode("//location[id=" & sID &
"]")
moDoc.DocumentElement.RemoveChild(oNode) <--oNode IS NULL AFTER
SEARCHING BY GUID
moDoc.Save(msFilePath)

End Function


<?xml version="1.0" encoding="utf-8"?>
<locations>
<location>
<id>e1156c58-f9fd-44aa-8f15-d6e7930bc524</id>
<name>Rosati's Pizza</name>
<address>
<street>1234 Main.</street>
<city>Chicago</city>
<state>IL</state>
<zip>60561</zip>
</address>
</location>
</locations>

Re: XPath Query Problem Oleg Tkachenko [MVP]
4/1/2004 11:50:38 AM
[quoted text, click to view]

Because later literal is considered as element name in XPath. Enclose
string literals in apostrophes.

[quoted text, click to view]
Should be
Dim oNode As XmlNode = moDoc.SelectSingleNode("//location[id='" & sID &
"']")

--
Oleg Tkachenko [XML MVP, XmlInsider]
AddThis Social Bookmark Button