PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Find an xml node

Reply

Find an xml node

 
Thread Tools Rate Thread
Old 29-03-2008, 06:00 PM   #1
Marco Trapanese
Guest
 
Posts: n/a
Default Find an xml node


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
  Reply With Quote
Old 29-03-2008, 08:55 PM   #2
Joe Cool
Guest
 
Posts: n/a
Default Re: Find an xml node

On Sat, 29 Mar 2008 18:00:38 GMT, Marco Trapanese
<marcotrapaneseNOSPAM@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?


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.
  Reply With Quote
Old 30-03-2008, 02:04 AM   #3
rowe_newsgroups
Guest
 
Posts: n/a
Default Re: Find an xml node

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]
  Reply With Quote
Old 30-03-2008, 06:41 AM   #4
Bill McCarthy
Guest
 
Posts: n/a
Default Re: Find an xml node

If this is a proper XML file it will have a root node these <book> nodes
will be children of. In that case you can use XDocument (VB9 (2008)) and
XLINQ:

Dim myDoc As XDcoument = <books>
<book id='1'>
<name></name>
<author></author>
</book>
<book id='2'>hello
</book>
<book id='3'>
</book>
</books>

Dim item = From x In myDoc.<book> Where x.@id = "2"



"Marco Trapanese" <marcotrapaneseNOSPAM@gmail.com> wrote in message
news:atvHj.277716$%k.397199@twister2.libero.it...
> 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


  Reply With Quote
Old 30-03-2008, 07:23 AM   #5
Stephany Young
Guest
 
Posts: n/a
Default Re: Find an xml node

What a load of rubbish!!!!!!!!!!!

It's as simple as:

_doc.SelectSingleNode("//book[@id='2']")

where _doc is the 'loaded' XmlDocument object.


"Joe Cool" <joecool@home.net> wrote in message
news:l0btu31f88b504dvf5akktrsbmq1b7phta@4ax.com...
> On Sat, 29 Mar 2008 18:00:38 GMT, Marco Trapanese
> <marcotrapaneseNOSPAM@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?

>
> 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.


  Reply With Quote
Old 30-03-2008, 11:05 AM   #6
Marco Trapanese
Guest
 
Posts: n/a
Default Re: Find an xml node

Stephany Young ha scritto:

> What a load of rubbish!!!!!!!!!!!
>
> It's as simple as:
>
> _doc.SelectSingleNode("//book[@id='2']")
>
> where _doc is the 'loaded' XmlDocument object.



Yeah, it really works!

Thanks!
Marco / iw2nzm
  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off