Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / August 2005

Tip: Looking for answers? Try searching our database.

traverse xml document nodes - SelectNodes()

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stephen - 26 Aug 2005 19:51 GMT
I have the following code that uses xquery to return search results
from an xml doc.

dim xmlString as string

xmlString =
"siteMapNode/siteMapNode/siteMapNode/siteMapNode[contains(translate(@title,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
'" + txtSearchCriteria.Text.ToUpper() + "')]"
               nodeList=root.SelectNodes(xmlString,nsmgr)

It all works great as long as I specify the level in which to search:

siteMapNode/siteMapNode/siteMapNode/siteMapNode

but I want to search all levels of the doc.

siteMapNode
siteMapNode/siteMapNode
siteMapNode/siteMapNode/siteMapNode

etc..

will I have to loop for each level and append the nodes:

dim myNode as string
dim myNodeLevel as string
do while something
myNodeLevel = myNodeLevel & "/siteMapNode"
loop

xmlString =
myNodeLevel &
"[contains(translate(@title,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
'" + txtSearchCriteria.Text.ToUpper() + "')]"
               nodeList=root.SelectNodes(xmlString,nsmgr)

anyway, that is air code but hopefully you'll get my point.  Any help
is appreciated.
Stephen - 26 Aug 2005 20:04 GMT
I did figure something out.  Here is what I did:

               dim doc as XmlDocument = new XmlDocument()
               doc.Load(Server.MapPath("web.sitemap"))

               dim nodeList as XmlNodeList
               dim root as XmlNode = doc.DocumentElement

               dim nsmgr as XmlNamespaceManager = new
XmlNamespaceManager(doc.NameTable)
               nsmgr.AddNamespace("ns0",
"xmlns:ns0='urn:schemas-microsoft-com:xslt")

               dim nodeLevel as string = ""
               dim i as integer
               for i = 1 to 4
                   nodeLevel = nodeLevel & "siteMapNode/"
                   dim xmlString as string = nodeLevel &
"siteMapNode[contains(translate(@title,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
'" + txtSearchCriteria.Text.ToUpper() + "')]"
                   'dim xmlString as string =
"siteMapNode/siteMapNode/siteMapNode/siteMapNode[contains(translate(@title,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
'" + txtSearchCriteria.Text.ToUpper() + "')]"
                   nodeList=root.SelectNodes(xmlString,nsmgr)

                   'Change the price on the books.
                   dim book as XmlNode
                   for each book in nodeList
                       Dim tempRow As New TableRow()
                       Dim tempCell As New TableCell()
                       tempCell.Text = "<a class='HoverStyle' href=" &
book.Attributes("url").Value & " target='_blank' a>" &
book.Attributes("title").Value & "</a>"
                       tempRow.Cells.Add(tempCell)
                       tblSearchResults.Rows.Add(tempRow)
                   next
               next i

It loops four levels of the xml doc.  Although I would like to be able
to determone the number of levels without hard-coding.  I will have to
research this.
Martin Honnen - 28 Aug 2005 12:34 GMT
> It all works great as long as I specify the level in which to search:
>
[quoted text clipped - 5 lines]
> siteMapNode/siteMapNode
> siteMapNode/siteMapNode/siteMapNode

Then you need the XPath expression
 //siteMapNode
to search from the document root node or
 .//siteMapNode
to search from the context node.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Stephen - 30 Aug 2005 18:27 GMT
Very nice.  Worked perfectly.  I can get rid of the loop now.  Thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.