Sign In/My Account | View Cart  
advertisement

Article:
 From JDOM to XmlDocument
Subject: XPathNavigator
Date: 2002-04-04 07:02:21
From: Niel Bornstein
Response to: XPathNavigator

You've asked two questions here, and I'll take them one at a time.


First, I used that convoluted bit of code to select nodes from XPathNavigator because that's what the .NET Framework Class Library Reference said to do, in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmlnodeclassselectnodestopic.asp.


As for your second point, well, yes, XmlNode.SelectNodes() would have been much simpler. I am guilty of not yet having read enough of the documentation.


Thanks for pointing it out!


No Previous Message Previous Message Move up to Parent Message Up Next Message No Next Message


Titles Only Titles Only Newest First
  • Revised SearchForArtist() method
    2002-04-05 07:29:15 Niel Bornstein

    After a bit more research, here is another way to write the SearchForArtist() method. You'll notice that it does not use XPathNavigator directly, however it achieves the same result as my original code and is, I think, a little clearer to read.


    public void SearchForArtist(string artist) {


    XmlNode root = document.DocumentElement;


    XmlNodeList nodeList = root.SelectNodes("//CD[@artist=normalize-space('" + artist + "')]");


    Console.WriteLine("CDs by {0}:", artist);


    foreach (XmlElement cd in nodeList) {


    Console.WriteLine(" \"{0}\"", cd.GetAttribute("title"));


    }


    }


Sponsored By: