VB.Net/XML/Read XML File

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

Output xml file to comma delimited Data

Imports System
Imports System.Xml.Linq
Imports Microsoft.VisualBasic.FileIO
Imports System.Text
Imports System.IO
    Public Class MainClass
        Public Shared Sub Main(ByVal args As String())
            Dim employees As XElement = XElement.Load("xmlFile.xml")
            Dim delimitedData As New StringBuilder
            Dim xmlData = _
                From emp In employees.<Employee> _
                    Select _
                    String.Format("""{0}"",""{1}"",""{2}"",""{3}"",""{4}""", _
                        emp.@id, emp.<Name>.Value, _
                        emp.<Title>.Value, emp.<HireDate>.Value, _
            For Each row In xmlData
            File.WriteAllText("data.txt", delimitedData.ToString)
        End Sub
    End Class

Reading an XML document

Imports System.IO
Imports System.Xml
Public Class MainClass
   Shared Sub Main()
      Dim document As XmlDocument = New XmlDocument()
      " create XmlNodeReader for document
      Dim reader As XmlNodeReader = New XmlNodeReader(document)
      " display each node"s content
      While reader.Read
         Select Case reader.NodeType
            " if Element, display its name
         Case XmlNodeType.Element
               " increase tab depth
               Console.WriteLine("<" & reader.Name & ">" )
               " if empty element, decrease depth
               If reader.IsEmptyElement Then
                   Console.WriteLine("Empty Element")
               End If
            Case XmlNodeType.rument " if Comment, display it
               Console.WriteLine("<!--" & reader.Value & _
                  "-->" )
            Case XmlNodeType.Text " if Text, display it
               Console.WriteLine(reader.Value )
               " if XML declaration, display it
            Case XmlNodeType.XmlDeclaration
               Console.WriteLine("<?" & reader.Name & " " & _
                  reader.Value & "?>" )
               " if EndElement, display it and decrement depth
            Case XmlNodeType.EndElement
               Console.WriteLine("</" & reader.Name & ">" )

         End Select
      End While
   End Sub " Main
End Class