Mega Code Archive

 
Categories / VB.Net / XML LINQ
 

Extensions AncestorsAndSelf

Imports System Imports System.Xml Imports System.Xml.XPath Public Class MainClass     Public Shared Sub Main()         Dim xmlTree As XElement = _              <Root>                 <Child1>                     <GrandChild1>                         <GreatGrandChild1>content</GreatGrandChild1>                     </GrandChild1>                 </Child1>                 <Child2>                     <GrandChild2>                         <GreatGrandChild2>content</GreatGrandChild2>                     </GrandChild2>                 </Child2>             </Root>                           Dim greatGrandChildren = From el In xmlTree.Descendants _                                  Where el.Name.LocalName.StartsWith("Great") _                                  Select el                Dim allAncestors = From el In greatGrandChildren.AncestorsAndSelf.Distinct _                            Select el                  For Each de As XElement In allAncestors             Console.WriteLine(de.Name)         Next     End Sub End Class