473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

traverse xml document nodes - SelectNodes()

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,'abcdefghijklmnopqrstuvw xyz','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,'abcdefghijklmnopqrstuvw xyz','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.

Nov 12 '05 #1
3 6494
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,'abcdefghijklmnopqrstuvw xyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
'" + txtSearchCriteria.Text.ToUpper() + "')]"
'dim xmlString as string =
"siteMapNode/siteMapNode/siteMapNode/siteMapNode[contains(translate(@title,'abcdefghijklmnopqrstuvw xyz','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.

Nov 12 '05 #2


Stephen wrote:

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


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

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #3
Very nice. Worked perfectly. I can get rid of the loop now. Thanks

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andy Fish | last post by:
Hi, I'm using XML DOM in Java and I have a function like this Element makeSomeXml(Document doc) {..} This returns an Element owned by (but not appended to) the specified document. Now,...
1
by: james.kingston | last post by:
In my first experimentation with js, I'm writing a greasemonkey script which adds links to a page which, when clicked, will replace their parent element with the contents of an element from another...
1
by: Praveen Naregal | last post by:
hello all, I have a problem. I have an XML file like below one.I want to loop through node by node and store or display the data . Can anybody give me a sample piece of c# code? Thanks &...
8
by: e-mid | last post by:
i want to get childs of specific xml node. normally i use xmlNodeList fooList = myNode.SelectNodes("foo"); but in compactframework , there is no selectNodes() method or selectSingleNode()...
6
by: J Williams | last post by:
I'm using axWebBrowser control and HTML DOM in a VB .NET Windows application to create a new HTML document by cloning nodes. The function below is called from the axWebBrowser1_DocumentComplete...
3
by: sfeher | last post by:
Hi All, The following code returns a valid xmlDoc (since I can evaluate and selectNodes) but its value is "xmlDoc= null" ?! Or at least this is what the FireBug shows and (xmlDoc===null) is...
1
by: Daniel Rucareanu | last post by:
Hello, Does anybody knows how can you delete, in just one step, not using a loop, a subset of the child nodes of a given DOM parent node? The subset will be continous, so for example, if the...
14
by: drdeath89 | last post by:
Im writing some Javascript code that requires me to loop all child nodes and sub child nodes of a certain parent node and execute a function with the child node in question. I decided to write a...
6
by: Premal Mishra | last post by:
How to traverse a binary tree whose nodes only contain a father field. e.g. struct NodeType { int data; int father; }; A left son's father field has a negative value and right son's father...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.