title: Get attribute values from matching XML nodes using XPath query
body: <p>This doesn't seem like it should be difficult, but I'm stuck currently.  I'm trying to get the attribute values for a particular attribute from nodes that match a given XPath query string.  Here's what I have so far:</p>
  
  <pre><code>    public static IEnumerable&lt;string&gt; GetAttributes(this XmlDocument xml,
          string xpathQuery, string attributeName)
      {
          var doc = new XPathDocument(new XmlNodeReader(xml));
          XPathNavigator nav = doc.CreateNavigator();
          XPathExpression expr = nav.Compile(xpathQuery);
          XPathNodeIterator iterator = nav.Select(expr);
          while (iterator.MoveNext())
          {
              XPathNavigator curNav = iterator.Current;
              if (curNav.HasAttributes)
              {
                  XmlNode curNode = ((IHasXmlNode)curNav).GetNode();
                  if (null != curNode)
                  {
                      XmlAttribute attrib = curNode.Attributes[attributeName];
                      if (null != attrib)
                      {
                          yield return attrib.Value;
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>This currently throws an exception:</p>
  
  <blockquote>
    <p>System.InvalidCastException: Unable to cast object of type 'MS.Internal.Xml.Cache.XPathDocumentNavigator' to type 'System.Xml.IHasXmlNode'.</p>
  </blockquote>
  
  <p>Am I going about this wrong?  Is there a simpler way to get attribute values from matching nodes?</p>