A couple of days ago I posted something on an XmlEnumerable. An object that knows how to traverse an XML document in a linear form. After talking with Adam, he suggested that I simplify the implementation with a little XPath action.

 1 public class XmlElementEnumerable : IEnumerable<IXmlElement> {
 2       private XmlElement rootElement;
 3       private IMapper<XmlElement, IXmlElement> mapper;
 4   
 5       public XmlElementEnumerable(XmlElement rootElement) {
 6           this.rootElement = rootElement;
 7           mapper = new XmlElementMapper();
 8       }
 9   
10       public IEnumerator<IXmlElement> GetEnumerator() {
11           foreach (var node in rootElement.SelectNodes("//*")) {
12               yield return mapper.MapFrom(node.DownCastTo<XmlElement>());
13           }
14       }
15   
16       IEnumerator IEnumerable.GetEnumerator() {
17           return GetEnumerator();
18       }
19   }

Diving a little deeper, I think using XPath expressions are probably a lot more efficient for traversing a document.

comments powered by Disqus