Groups | Blog | Home
all groups > dotnet xml > october 2003 >

dotnet xml : Simple xpath expression problem


Kirk
10/3/2003 9:52:56 AM
I am trying to use a simple xpath expression, the code looks like this:

Dim xdoc As New System.Xml.XmlDocument
Dim xnode1 As System.Xml.XmlNode
Dim xlist As System.Xml.XmlNodeList

xdoc.LoadXml(Application("MenuXML"))

xlist = xdoc.DocumentElement.SelectNodes("//MenuItem[@User=1]")

For Each xnode1 In xlist
xnode1.ParentNode.RemoveChild(xnode1)
Next

the xml document looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<MenuRoot>
<Page ID="101">
<MenuData ImagesBaseURL="images/" xmlns="ASP.NET Menu MenuData Schema">
<MenuGroup>
<MenuItem Label="Home" ID="1" User="0"></MenuItem>
<MenuItem Label="Reports" User="1">
<MenuGroup ExpandOffsetY="1" ExpandOffsetX="-1">
<MenuItem Label="Department" ID="7" />
<MenuItem Label="Division" ID="8" />
</MenuGroup>
</MenuItem>
</MenuGroup>
</MenuData>
</Page>
</MenuRoot>

can anyone tell me what is wrong with my xpath expression that it is not
finding the MenuItem with User=1? More specifically, why it is that xlist
has no nodes in it. I surely appreciate any help.

Thank You in advance, (I will likely thank you again later, though as well!)

Kirk


Kirk
10/4/2003 10:28:31 AM
Ahhhhh!,

thank you Oleg. that was my problem!

Kirk

[quoted text, click to view]

Oleg Tkachenko
10/4/2003 12:19:04 PM
[quoted text, click to view]

'Create an XmlNamespaceManager for resolving namespaces.
Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(xdoc.NameTable)
nsmgr.AddNamespace("foo", "ASP.NET Menu MenuData Schema")

[quoted text, click to view]
xlist = xdoc.DocumentElement.SelectNodes("//MenuItem[@User=1]", nsmgr)

Search "XPath and default namespace" in archives for detail explanation,
this is the faqest faq.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
AddThis Social Bookmark Button