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 / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

SelectNodes twice

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sam - 13 Sep 2007 20:58 GMT
I need to make a SelectNodes and in the result set make a second
SelectNodes:

XmlNodeList nodeList = doc.DocumentElement.SelectNodes(xPathQuery);
Now I need to make a second .SelectNodes(xPathQuery);

How can I do that because nodeList does not have a SelectNodes method ?

Thanks for help.
Sam
Marc Gravell - 13 Sep 2007 21:31 GMT
Can you give an example of the 2 queries? Most times, xpath can cope
with both at once... it is very flexible ;-p

If this isn't possible (for some context-specific reason) then perhaps
you might have to enumerate the first list, evaluating the 2nd xquery,
and dumping the successive results into a list.

Finally - remember that you can (in some scenarios) add helper
(extension) objects to the mix, so that you can include arbitrary C#
code in your expression - i.e. "freds:superMethod(.)" (where "freds"
is an alias to a namespace that represents your extension object).

Marc
sam - 13 Sep 2007 21:46 GMT
> Can you give an example of the 2 queries? Most times, xpath can cope
> with both at once... it is very flexible ;-p
I use in my csharp code this xml:
<doc>
<item Id="1">
     <item>1</item>
</item>
<item Id="1">
     <item>2</item>
</item>
<item Id="2">
    <item>A</item>
</item>
<item Id="3">
   <item>B</item>
</item>
</doc>

I use this /doc/item[@Id != "1"  and (position() >0 and position() <= 2) ]
but there is no result, why ?
Sam
Marc Gravell - 13 Sep 2007 21:56 GMT
xquery is 1-based (not 0-based like C#); nodes 1 & 2 have @Id = "1",
so are excluded. nodes 3 & 4 (@Id = 2, 3) fail because of the
position().

I'll double check with an example... give me a sec...

Marc
Marc Gravell - 13 Sep 2007 21:59 GMT
This returns node A; is this what you are after?

       doc.LoadXml(xml);
       foreach (XmlNode node in doc.SelectNodes(@"/doc/item[@Id !
=""1"" and (position() > 1 and position() <= 3)]")) {
           Console.WriteLine(node.OuterXml);
       }
sam - 13 Sep 2007 22:21 GMT
> xquery is 1-based (not 0-based like C#); nodes 1 & 2 have @Id = "1",
> so are excluded. nodes 3 & 4 (@Id = 2, 3) fail because of the
> position().
Okey but my needs is to get from the result set a range of nodes that start
at the first position, because I don't know by advance how many nodes have
been excluded.

Sam
Marc Gravell - 14 Sep 2007 05:24 GMT
> Okey but my needs is to get from the result set a range of nodes that start
> at the first position, because I don't know by advance how many nodes have
> been excluded.

Oh, that's even easier; for the first only:
@"/doc/item[@Id ! =""1""][1]

For the first 2:
@"/doc/item[@Id ! =""1""][position() <= 2]

Fortunately, position() refers to the position *within the current
working set*, and not the fixed position in the document.

Gotta love it,

Marc

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.