Mega Code Archive

 
Categories / VB.Net / XML
 

Extensions DescendantNodes(T)

Imports System Imports System.Xml Imports System.Xml.XPath Imports System.Collections Imports System.Collections.Generic Public Class MainClass     Public Shared Sub Main()                  Dim xmlTree As XElement = _         <Root>             <Child>aaa<GrandChild anAttribute='xyz'>Text</GrandChild>                 <!--a comment-->                 <?xml-stylesheet type='text/xsl' href='test.xsl'?>             </Child>             <Child>ccc<GrandChild>Text</GrandChild>ddd</Child>         </Root>                  Dim nodes As IEnumerable(Of XNode) = _             From node In xmlTree.<Child>.DescendantNodes _             Select node                  For Each node As XNode In nodes             Select Case node.NodeType                 Case XmlNodeType.Element                     Console.WriteLine("Element: {0}", DirectCast(node, XElement).Name)                 Case XmlNodeType.Text                     Console.WriteLine("Text: {0}", DirectCast(node, XText).Value)                 Case XmlNodeType.Comment                     Console.WriteLine("Comment: {0}", DirectCast(node, XComment).Value)                 Case XmlNodeType.ProcessingInstruction                     Console.WriteLine("PI: {0}", DirectCast(node, XProcessingInstruction).Data)             End Select         Next     End Sub End Class