VB.Net/XML LINQ/Descendants

Материал из VB Эксперт
Перейти к: навигация, поиск

Getting Descendants

  
Imports System
Imports System.Reflection
Imports System.Xml
Module Module1
    Sub Main()
        Dim xml As XElement = XElement.Load("People.xml")
        Dim record = xml.Descendants("firstname").First()
        For Each tag In record.Ancestors()
            Console.WriteLine(tag.Name)
        Next

    End Sub
End Module


Query all Ford cars

  
Module Program
  Sub Main()
    Dim doc As XElement = XElement.Load("Inventory.xml")
    Dim fords = From c In doc.Descendants("Make") _
                Where c.Value = "Ford" _
                Select c
    For Each f In fords
      Console.WriteLine(f)
    Next
  End Sub
End Module


Select all Descendants

  
Module Program
  Sub Main()
    Dim doc As XElement = XElement.Load("Inventory.xml")
    Dim petNames = From pn In doc.Descendants("PetName") Select pn.Value
    For Each name In petNames
      Console.WriteLine("Name: {0}", name)
    Next
  End Sub
End Module


Select nodes from Descendants

  
Imports System
Imports System.Data.DLinq
Imports System.Expressions
Imports System.Query
Imports System.Reflection
Imports System.Xml
Module Module1
    Sub Main()
        Dim xdoc As XDocument = XDocument.Load("data.xml")
        Dim query = From people In xdoc.Descendants("PERSON") Select people.Value
        Console.WriteLine("{0} Players Found", query.Count())
        For Each item In query
            Console.WriteLine(item)
        Next

    End Sub
End Module


Using Linq to get default in a Office ord document

  
Imports System
Imports System.Reflection
Imports System.Xml
Module Module1
    Sub Main()
        Dim xml As XElement = XElement.Load("XLINQ.xml")
        Dim w As XNamespace = "http://schemas.microsoft.ru/office/word/2003/wordml"
        Dim defaultFonts As XElement = xml.Descendants(w + "defaultFonts").First()
        Console.WriteLine("Default Fonts: {0}", _
            defaultFonts.Attribute(w + "ascii").Value)
    End Sub
End Module