BizTalk Utilities CV ,   Jobs ,   Code library
 
Go to the front page to continue learning about XML or select below:

Contents

ReBlogger Contents

Previous posts in .NET XML, System.XML

 
 
Page 17501 of 20217

foreach and XPathNodeIterator - finally together

Blogger : Oleg Tkachenko
All posts : All posts by Oleg Tkachenko
Category : .NET XML, System.XML
Blogged date : 2005 May 09

This one little improvement in System.Xml 2.0 Beta2 is sooo cool anyway: XPathNodeIterator class at last implements IEnumerable! Such unification with .NET iteration model means we can finally iterate over nodes in an XPath selection using standard foreach statement:

XmlDocument doc = new XmlDocument();
doc.Load("orders.xml");
XPathNavigator nav = doc.CreateNavigator();
foreach (XPathNavigator node in nav.Select("/orders/order"))
    Console.WriteLine(node.Value);
Compare this to what we have to write in .NET 1.X:
XmlDocument doc = new XmlDocument();
doc.Load("../../source.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator ni = nav.Select("/orders/order");
while (ni.MoveNext())      
  Console.WriteLine(ni.Current.Value);
Needless to say - that`s the case when just a dozen lines of code can radically simplify a class`s usage and improve overall developer`s productivity. How come this wasn`t done in .NET 1.1 I have no idea.

And how come the MSDN documentation for the class still doesn`t mention this cool feature - I have no idea either.


Read comments or post a reply to : foreach and XPathNodeIterator - finally together
Page 17501 of 20217

Newest posts
 

    Email TopXML