Sign In/My Account | View Cart  
advertisement

Article:
 From JDOM to XmlDocument
Subject: XPathNavigator
Date: 2002-04-04 06:45:21
From: guy lukes

Wouldn't it have been easier to use


iterator.Current.GetAttribute("title", "")
than


XPathNavigator nav2 = iterator.Current.Clone();
((IHasXmlNode)nav2).GetNode().Attributes[0].Value


or simply use the SelectNodes method of XmlNode


No Previous Message Previous Message   Next Message Next Message


Titles Only Titles Only Newest First
  • XPathNavigator
    2002-04-04 07:02:21 Niel Bornstein

    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!

    • 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: