VB.Net/XML LINQ/XDocument — различия между версиями

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

Текущая версия на 12:45, 26 мая 2010

Add element to XDocument

  
Imports System
Imports System.Reflection
Imports System.Xml
Module Module1
    Sub Main()
        Dim xdoc As XDocument = XDocument.Load("C:\hamlet.xml")
        Dim xe As XElement = New XElement("PERSONA","new value")
        xdoc.Element("PLAY").Element("PERSONAE").Add(xe)
        Dim query = From people In xdoc.Descendants("PERSONA") Select people.Value
        Console.WriteLine("{0} Players Found", query.Count())
        For Each item In query
            Console.WriteLine(item)
        Next
    End Sub
End Module


Create an in-memory XML document with XDocument

  
Class Car
  Public PetName As String
  Public ID As Integer
End Class
Module Program
  Sub Main()
    " .
    Dim inventoryDoc As XDocument = _
  <?xml version="1.0" encoding="utf-8" standalone="yes"?>
  <!--Current Inventory of AutoLot-->
  <Inventory>
    <Car ID="1">
      <Color>Green</Color>
      <Make>BMW</Make>
      <PetName>Stan</PetName>
    </Car>
    <Car ID="2">
      <Color>Pink</Color>
      <Make>Yugo</Make>
      <PetName>A</PetName>
    </Car>
  </Inventory>
    Console.WriteLine(inventoryDoc)
    inventoryDoc.Save("SimpleInventory.xml")
  End Sub

End Module


Create new Xml document with XDocument and XElement

  
Imports System
Imports System.Reflection
Imports System.Xml
Module Module1
    Sub Main()
        Dim xml1 As New XDocument(New XDeclaration("1.0", "UTF-8", "yes"), _
                    New XElement("people", _
                    New XAttribute("id", 1), _
                    New XAttribute("year", 2004), _
                    New XAttribute("salar", "1")))
        Dim sw As New System.IO.StringWriter()
        xml1.Save(sw)
        Console.WriteLine(sw)
    End Sub
End Module


Create XDocument from XmlReader

  
Imports System
Imports System.Reflection
Imports System.Xml
Module Module1
    Sub Main()
        Dim reader As XmlReader = XmlReader.Create("People.xml")
        Dim xml As XDocument = XDocument.Load(reader)
        Console.WriteLine(xml)
        Dim idperson As XElement = xml.Descendants("person").Last()
        idperson.Add(New XElement("id", _
                        New XAttribute("id", 1), _
                        New XAttribute("year", 2006), _
                        New XAttribute("salary", "1")))
        Dim sw As New IO.StringWriter()
        Dim w As XmlWriter = XmlWriter.Create(sw)
        xml.Save(w)
        w.Close()
        Console.WriteLine(sw.ToString())
    End Sub
End Module


Display the name of the root element in the loaded XML tree

  
Imports System
Imports System.Xml.Linq
    Public Class MainClass
        Public Shared Sub Main()
            Dim xmlDoc As XDocument = XDocument.Load("Employees.xml")
            Console.WriteLine("The document declaration is "{0}"", xmlDoc.Declaration.ToString)
            Console.WriteLine("The root element is "{0}"", xmlDoc.Root.Name.LocalName)
        End Sub
    End Class


Display the XML files declaration information with an XDocument object

  
Imports System
Imports System.Xml.Linq
    Public Class MainClass
        Public Shared Sub Main()
            Dim xmlDoc As XDocument = XDocument.Load("Employees.xml")
            Console.WriteLine("The document declaration is "{0}"", xmlDoc.Declaration.ToString)
            Console.WriteLine("The root element is "{0}"", xmlDoc.Root.Name.LocalName)
        End Sub
    End Class


Get version from XDocument

  
Imports System
Imports System.Reflection
Imports System.Xml
Module Module1
    Sub Main()
        Dim xml As XDocument = XDocument.Load("Hello_XLINQ.xml")
        Console.WriteLine("Version: {0}", xml.Declaration.Version)
    End Sub
End Module


Get Xml encoding from XDocument

  
Imports System
Imports System.Reflection
Imports System.Xml
Module Module1
    Sub Main()
        Dim xml As XDocument = XDocument.Load("Hello_XLINQ.xml")
        Console.WriteLine("Encoding: {0}", xml.Declaration.Encoding)
    End Sub        
End Module


Is Xml standalone

  
Imports System
Imports System.Reflection
Imports System.Xml
Module Module1
    Sub Main()
        Dim xml As XDocument = XDocument.Load("Hello_XLINQ.xml")
        Console.WriteLine("Standalone: {0}", xml.Declaration.Standalone)
    End Sub        
End Module


Load the Employees.xml and store the contents into an XDocument object

  
Imports System
Imports System.Xml.Linq
    Public Class MainClass
        Public Shared Sub Main()
            Dim xmlDoc As XDocument = XDocument.Load("Employees.xml")
            Console.WriteLine("The document declaration is "{0}"", xmlDoc.Declaration.ToString)
            Console.WriteLine("The root element is "{0}"", xmlDoc.Root.Name.LocalName)
        End Sub
    End Class


LocalName returns the name of the element as a string

  

Imports System
Imports System.Xml.Linq
    Public Class MainClass
        Public Shared Sub Main()
            Dim xmlDoc As XDocument = XDocument.Load("Employees.xml")
            Console.WriteLine("The document declaration is "{0}"", xmlDoc.Declaration.ToString)
            Console.WriteLine("The root element is "{0}"", xmlDoc.Root.Name.LocalName)
        End Sub
    End Class


Name property returns the XName class

  
Imports System
Imports System.Xml.Linq
    Public Class MainClass
        Public Shared Sub Main()
            Dim xmlDoc As XDocument = XDocument.Load("Employees.xml")
            Console.WriteLine("The document declaration is "{0}"", xmlDoc.Declaration.ToString)
            Console.WriteLine("The root element is "{0}"", xmlDoc.Root.Name.LocalName)
        End Sub
    End Class


Reading Rss feed with XDocument

  
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("http://yourDomain.net/Rss.aspx")
        Dim query = From rssFeed In xdoc.Descendants("channel") _
                    Select Title = rssFeed.Element("title").Value, _
                       Description = rssFeed.Element("description").Value, _
                       Link = rssFeed.Element("link").Value
        For Each item In query
            Console.WriteLine("TITLE: " + item.Title)
            Console.WriteLine("DESCRIPTION: " + item.Description)
            Console.WriteLine("LINK: " + item.Link)
        Next

    End Sub
End Module