What, if anything, is the difference between these two code fragments?
XmlNode x = GetNode();
foreach (var n in x) {}
and
XmlNode x = GetNode();
foreach (var n in x.ChildNodes) {}
Does the indexer do anything that the ChildNodes doesn't?
Not much difference. From the source:
public IEnumerator GetEnumerator()
{
return new XmlChildEnumerator(this);
}
public virtual XmlNodeList ChildNodes
{
get { return new XmlChildNodes(this); }
}
And in the XmlChildNodes
class:
public override IEnumerator GetEnumerator()
{
if (_container.FirstChild == null)
{
return XmlDocument.EmptyEnumerator;
}
else
{
return new XmlChildEnumerator(_container);
}
}
So slightly more efficient to use the enumerator on the parent node directly.