Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6362657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:56:14+00:00 2026-05-24T23:56:14+00:00

I was under the impression that a node can be anything at all, be

  • 0

I was under the impression that a node can be anything at all, be it element, attribute, etc etc.

I was looking at trying to iterate through a node list, eg ….

Dim xmlDoc As New Xml.XmlDocument
xmlDoc.LoadXml("   
    < main1 test1 = 'any' test2 = 'any2' >
    < test1 > ttr < /test1 >
    < test1 > ttr < /test1 >
    < test1 > ttr < /test1 >
    < test1 > ttr < /test1 >
    < /main1 >")

Question 1

Why does the following return only the elements, and not the attributes:

For Each objnode As Xml.XmlNode In xmlDoc.DocumentElement.ChildNodes
    Console.WriteLine(objnode.Name)
Next

Question 2

How can I iterate through all nodes, regardless of type, using xpath?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:56:15+00:00Added an answer on May 24, 2026 at 11:56 pm

    Well in the DOM model that System.Xml.XmlDocument/XmlElement/XmlNode implement the ChildNodes collection gives you all nodes considered child nodes of the container node, that can be element nodes, text nodes, comment nodes, processing instruction nodes (and for the XmlDocument a DOCTYPE node and the XML declaration). Attribute nodes are not considered child nodes in that model so you won’t find them in the ChildNodes collection. You can access the Attributes collection if you are interested.

    [edit]
    Well you can use the XPath union operator | to select and process the union of nodes so with the XML being

    <!-- comment 1 -->
    <root att1="value 2" att2="value 2">Text<!-- comment 2 --><child att3="value 3">
      <descendant/>
    </child>
    </root>
    

    the following snippet of VB

    Dim doc As New XmlDocument()
    doc.Load("file.xml")
    
    For Each node As XmlNode In doc.SelectNodes("//node() | //@*")
        Console.WriteLine("Found node of type {0} with name {1}", node.NodeType, node.Name)
    Next
    

    outputs

    Found node of type Comment with name #comment
    Found node of type Element with name root
    Found node of type Attribute with name att1
    Found node of type Attribute with name att2
    Found node of type Text with name #text
    Found node of type Comment with name #comment
    Found node of type Element with name child
    Found node of type Attribute with name att3
    Found node of type Element with name descendant
    

    So that way you have one path expression selecting both attributes as well as element nodes, text nodes and comment nodes.

    You should also be aware that DOM and XSLT/XPath have some mismatch in their tree models, for instance DOM distinguishes normal text nodes and CDATA section nodes, XPath does not do that. And the DOM allows adjacent text nodes, XPath does not do that. So while you can often write XPath queries against DOM trees and Microsoft supports that with both MSXML’s DOM implementation and .NET’s DOM implementation you need to be aware of subtle differences between the tree models XPath is defined against and you use XPath with when doing SelectSingleNode or SelectNodes in System.Xml.XmlDocument/XmlElement/XmlNode.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was under the impression that these days javascript can be used as a
I am under the impression that the best way to style a list item
I was under the impression that all updates to a SQL server database are
I was under impression that rm -r *.xml would remove all file from parent
I'm under the impression that changing an anchor tag on hover can be done
I was under the impression that all value types inherit from System.ValueType , and
I was under the impression that I could run it on any machine, but
I'm under the impression that the Dot '.' (wild card) character is dangerous to
I was under the impression that using the Resolve method returned a new instance
I was under the impression that I could put any old executable program in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.