|
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
De verschillende nodes en elementen van XML kunnen we op allerlei manieren benaderen. 28.5.1. System.Xml.Linq.XObjectZowel XAttributes als XNodes zijn XObjects. Een XObject hoort tot een bepaald XDocument en kan tot een parent XElement behoren. boven
28.5.2. System.Xml.Linq.XAttributeEen XAttribute is een XObject en stelt een attribuut voor van een XNode. boven
28.5.3. System.Xml.Linq.XNodeEen XNode is een XObject en stelt een node voor. Een XNode kan gekoppeld zijn aan een vorige en volgende XNode. Zowel XComments als XContainers zijn XNodes. boven
28.5.4. System.Xml.Linq.XCommentEen XComment is een XNode en stelt een commentaar node voor. boven
28.5.5. System.Xml.Linq.XContainerEen XContainer is een XNode en stelt een node voor die child nodes kan bevatten. Zowel XElements als XDocuments zijn XContainers. boven
28.5.6. System.Xml.Linq.XElementEen XElement is een XContainer en stelt een node voor die een waarde kan hebben. Deze waarde kan een reeks van child nodes zijn. Het XElement zelf kan een parent element zijn van andere XNodes. boven
28.5.7. System.Xml.Linq.XDocumentEen XDocument is een XContainer en stelt een XML document voor. Deze heeft steeds exact één root XElement. boven
28.5.8. System.Xml.Linq.XNameEen XName stelt de naam voor van ondermeer XElements en XAttributes. boven
28.5.9. Een VoorbeeldIn dit onderwerp stellen we volgend XML document op : <?xml version="1.0"?> <administration> <employees> <employee> <name>Alex Smith</name> </employee> <employee> <name>John Smith</name> <departement type="Sales">London</departement> <departement type="distribution">England</departement> </employee> <employee> <name>Jane Jones</name> <departement type="sales">Manchester</departement> </employee> </employees> </administration> We geven aan het document ( XDocument ), maar ook aan enkele elementen ( XElement ), attributen ( XAttribute ) en namen ( XName ) een naam. Dit om in onderstaand voorbeeld de mogelijkheden om deze te benaderen te kunnen illustreren : Visual Basic 2010 Broncode Option Explicit OnOption Strict OnImports System.Collections.Generic Imports System.Xml.Linq Namespace AccessingXML Class Example1 Public Shared Sub Main() Dim nameXElementValue As String = "John Smith" Dim nameXElement As XElement = <name><%= nameXElementValue %></name> Dim departementXElementValue As String = "London" Dim typeXAttributeValue As String = "Sales" Dim departementXName As XName = XName.Get( "departement") Dim typeXName As XName = XName.Get( "type") Dim typeXAttribute As XAttribute = _ New XAttribute(typeXName, typeXAttributeValue) Dim departement1XElement As XElement = _ <departement <%= typeXAttribute %>><%= departementXElementValue %></departement> Dim departement2XElement As XElement = _ <departement type= "distribution">England</departement> Dim employee1XElement As XElement = <employee> <name>Alex Smith</name> </employee> Dim employee2XElement As XElement = <employee> <%= nameXElement %> <%= departement1XElement %> <%= departement2XElement %> </employee> Dim employee3XElement As XElement = _ <employee> <name>Jane Jones</name> <departement type= "sales">Manchester</departement> </employee> Dim employeesXElement As XElement = <employees> <%= employee1XElement %> <%= employee2XElement %> <%= employee3XElement %> </employees> Dim administrationXElement As XElement = _ <administration> <%= employeesXElement %> </administration> Dim document As XDocument = <?xml version= "1.0"?> <%= administrationXElement %> Console.WriteLine(document.ToString()) Console.WriteLine(employee1XElement.Document Is document) Console.WriteLine(typeXAttribute.Document Is document) Console.WriteLine(employeesXElement.Name Is XName.Get( "employees")) Console.WriteLine(nameXElement.Value Is nameXElementValue) Console.WriteLine(typeXAttribute.Value Is typeXAttributeValue) Console.WriteLine(nameXElement.IsEmpty() = False) Console.WriteLine(employeesXElement.FirstNode Is employee1XElement) Console.WriteLine(employeesXElement.LastNode Is employee3XElement) Dim childNodes As IEnumerable( Of XNode) = employeesXElement.Nodes() childNodes = document.Nodes() Console.WriteLine( _ employee2XElement.Element(XName.Get( "departement")) Is _ departement1XElement) Dim elements As IEnumerable( Of XElement) elements = employeesXElement.Elements() elements = employeesXElement.Descendants() elements = employeesXElement.DescendantsAndSelf() elements = employee2XElement.Ancestors() elements = employee2XElement.AncestorsAndSelf() Console.WriteLine(employeesXElement.HasElements()) Console.WriteLine(employee1XElement.Parent Is employeesXElement) Console.WriteLine(employee1XElement.NextNode Is employee2XElement) Console.WriteLine(employee3XElement.NextNode Is Nothing) Console.WriteLine(employee2XElement.PreviousNode Is _ employee1XElement) Console.WriteLine(employee1XElement.PreviousNode Is Nothing) Console.WriteLine(employee1XElement.IsBefore(employee2XElement)) Console.WriteLine(employee2XElement.IsAfter(employee1XElement)) Console.WriteLine(departement1XElement.Attribute(typeXName) Is _ typeXAttribute) Dim attributes As IEnumerable( Of XAttribute) = _ departement1XElement.Attributes() Console.WriteLine(departement1XElement.FirstAttribute Is _ typeXAttribute) Console.WriteLine(departement1XElement.LastAttribute Is _ typeXAttribute) Console.WriteLine(departement1XElement.HasAttributes()) Console.ReadLine() End Sub End ClassEnd NamespaceDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output <administration>
<employees>
<employee>
<name>Alex Smith</name>
</employee>
<employee>
<name>John Smith</name>
<departement type="Sales">London</departement>
<departement type="distribution">England</departement>
</employee>
<employee>
<name>Jane Jones</name>
<departement type="sales">Manchester</departement>
</employee>
</employees>
</administration>
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
TrueWe hebben in een vorig voorbeeld al eens de voor <employee> child nodes <departement> via een LINQ query laten bepalen. In onderstaand voorbeeld doen we dit nogmaals, maar deze keer haalt de LINQ query zijn informatie uit de IEnumerable(Of XElement) collectie die de Elements functie van het XElement object departements oplevert : Visual Basic 2010 Broncode Namespace AccessingXML Class Example2 Public Shared Sub Main() Dim departements As XElement = _ <departements> <departement type= "Sales">London</departement> <departement type= "Distrubution">England</departement> <departement type= "Management">London</departement> </departements> Dim employee1 As XElement = _ <employee> <name>John Smith</name> <%= From d In departements.Elements() _ Where d.Value = "London" _ Select d %> </employee> Console.WriteLine(employee1.ToString()) Console.ReadLine() End Sub End Class Class Departement Protected _Area As String Public Property Area() As String Get Area = _Area End Get Set( ByVal value As String) _Area = value End Set End Property Protected _Type As DepartementType Public Property Type() As DepartementType Get Type = _Type End Get Set( ByVal value As DepartementType) _Type = value End Set End Property End Class Enum DepartementType Sales Distrubution End EnumEnd NamespaceDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output <employee>
<name>John Smith</name>
<departement type="Sales">London</departement>
<departement type="Management">London</departement>
</employee>
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
|