By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

Find an xml node

P: n/a
Hello,

I have a xml file like this:

<book id='1'>
<name></name>
<author></author>
...
</book>

<book id='2'>
...
</book>

<book id='3'>
...
</book>

....
How to find the book with attribute id = '2' without cycle all nodes?

Thanks!
Marco / iw2nzm
Mar 29 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sat, 29 Mar 2008 18:00:38 GMT, Marco Trapanese
<ma******************@gmail.comwrote:
>Hello,

I have a xml file like this:

<book id='1'>
<name></name>
<author></author>
...
</book>

<book id='2'>
...
</book>

<book id='3'>
...
</book>

...
How to find the book with attribute id = '2' without cycle all nodes?
Can't do it, as far I am aware. You will have to restrieve a NodeList
on book, then cycle through the nodes looking for the first one that
has an attribute of id with the value of 2.

Well , technically, you are not cycling thorugh all nodes, since the
one you are looking far, at least in this example, is the second one.
But you will need to first retrieve the entire list of book nodes with
a SelectNodes method invocation.

Besides, by this time you have the entire XML file in memory anyway
from the DOM Load method.
Mar 29 '08 #2

P: n/a
On Mar 29, 2:00*pm, Marco Trapanese <marcotrapaneseNOS...@gmail.com>
wrote:
Hello,

I have a xml file like this:

<book id='1'>
* * * * <name></name>
* * * * <author></author>
* * * * ...
</book>

<book id='2'>
* * * * ...
</book>

<book id='3'>
* * * * ...
</book>

...

How to find the book with attribute id = '2' without cycle all nodes?

Thanks!
Marco / iw2nzm
It would be possible to find the element using a string search, but it
would be very difficult to find the matching end element without some
serious Regex overhead. Then you would be able to load the string you
found into memory as xml and parse it normally. I seriously doubt this
would give you better performance, and would be a huge pain to
maintenance.

Why exactly are you afraid to loop through the nodes?

Thanks,

Seth Rowe [MVP]
Mar 30 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.