XPath: selectnodes does not return declaration or comment RRS feed

  • Question

  • In this example:

    <?xml version='1.0'?>
      <bookstore xmlns="urn:newbooks-schema">
        <book genre="novel" style="hardcover">
          <title>The Handmaid's Tale</title>
        <book genre="novel" style="other">
          <title>The Poisonwood Bible</title>
        <book genre="novel" style="paperback">
          <title>The Bean Trees</title>

    I try to read all nodes with:

     Public Function getAllNodes() As XmlNodeList
             getAllNodes = XDoc.SelectNodes("//*")
             'Is idem:
             'getAllNodes = XDoc.SelectNodes("descendant::*")
     End Function

    It works fine, but it does not return the declaration tag nor does it return (in other xml-files) the comment tag.

    These tags I get with looping through something like

    dim xmlKnoten as xml.xmlnode
    For Each xmlKnoten In XDoc.ChildNodes  
    Why is this so?

    Saturday, May 31, 2014 7:55 AM


  • To match any element and comment, try this: "//*|//comment()". To match any node, try "//node()". To get the main declaration, consider this too:

        XmlDeclaration decl = XDoc.OfType<XmlDeclaration>().FirstOrDefault();

    Saturday, May 31, 2014 1:52 PM