VB.Net/XML/XML File Read

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

Is starting element

  
Imports System.Xml
Public Class Example4
    Shared Sub Main
        Dim reader As XmlReader
        Dim totalPrice As Double = 0
        reader = XmlReader.Create("pubs.xml")
        While reader.Read()
            If reader.IsStartElement() = True And reader.Name = "titles" Then
                reader.MoveToAttribute("price")
                totalPrice += reader.ReadContentAsDouble()
            End If
        End While
    End Sub
End Class


Load Xml document and print attributes

 
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.Data
Imports System.Windows.Forms

Public Class MainClass
    
    Shared Sub Main()
        Dim Doc As New XmlDocument()
        Dim TempNode As XmlElement
        Dim x As Integer
                
        Doc.Load("OutDocument.xml")
        Dim DocRoot As XmlElement = Doc.CreateElement("Orders")
        For x = 0 To DocRoot.ChildNodes.Count - 1
            TempNode = CType(DocRoot.ChildNodes(x), XmlElement)
            Console.WriteLine("Order Quantity: {0}", _
                 TempNode.GetAttribute("Quantity"))
        Next
    End Sub
End Class


Reading an XML file example

  
Module MainModule
    Sub Main()
        Dim MyReader As New Xml.XmlTextReader("c:\simple.xml")
        Dim strOutput As String, intCount As Integer
        Do While MyReader.Read
            Select Case MyReader.NodeType
                Case Xml.XmlNodeType.Element
                    System.Console.WriteLine(MyReader.Name)
                    If MyReader.HasAttributes Then
                        For intCount = 0 To MyReader.AttributeCount - 1
                            MyReader.MoveToAttribute(intCount)
                            System.Console.WriteLine(MyReader.Name & "=" & MyReader.Value)
                        Next
                    End If
                Case Xml.XmlNodeType.Text
                    System.Console.WriteLine(MyReader.Value)
                Case Xml.XmlNodeType.EndElement
                    System.Console.WriteLine(MyReader.Name)
            End Select
        Loop
    End Sub
End Module


Use XML Reader to display XML tag, Node type and attributes

 
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO

Public Class MainClass
   Public Shared Sub Main()
        Dim myXmlSettings As New XmlReaderSettings()
        Using readMovieInfo As XmlReader = XmlReader.Create("test.xml", myXmlSettings)
            While readMovieInfo.Read()
                ShowXmlNode(readMovieInfo)
                While readMovieInfo.MoveToNextAttribute()
                    ShowXmlNode(readMovieInfo)
                End While
            End While
        End Using
   End Sub
   Shared Private Sub ShowXmlNode(ByVal reader As XmlReader)
        If reader.Depth > 0 Then
            For depthCount As Integer = 1 To reader.Depth
                Console.Write(" ")
            Next
        End If
        If reader.NodeType = XmlNodeType.Whitespace Then
            Console.WriteLine("Type: {0} ", reader.NodeType)
        ElseIf reader.NodeType = XmlNodeType.Text Then
            Console.WriteLine("Type: {0}, Value: {1} ",reader.NodeType,reader.Value)
        Else
            Console.WriteLine("Name: {0}, Type: {1}, AttributeCount: {2}, Value: {3} ", _
                                 reader.Name, reader.NodeType,reader.AttributeCount, _
                                 reader.Value)
        End If
    End Sub
End Class


Using XmlReaderSettings and XmlWriterSettings

  
Imports System.Xml
Public Class Example2
    Shared Sub Main()
        Dim reader As XmlReader
        Dim writer As XmlWriter
        Dim readerSettings As New XmlReaderSettings()
        Dim writerSettings As New XmlWriterSettings()
        readerSettings.IgnoreComments = True
        readerSettings.Schemas.Add(Nothing, "pubs.xsd")
        readerSettings.ValidationType = ValidationType.None
        writerSettings.OmitXmlDeclaration = True
        writerSettings.Indent = True
        writerSettings.NewLineOnAttributes = True
        reader = XmlReader.Create("pubs.xml", readerSettings)
        writer = XmlWriter.Create("output.xml", writerSettings)
        While reader.Read()
            writer.WriteNode(reader, True)
        End While
        reader.Close()
        writer.Close()
    End Sub
End Class


XmlTextReader: XmlNodeType Element, Text and EndElement

 
Imports System
Imports System.Drawing
Imports System.Data
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Xml
Public Class MainClass
    Shared Sub Main()
        Dim filename As String = "book.xml"
        Dim reader As New XmlTextReader(filename)
        reader.MoveToContent()
        Dim addressData As Collection, elementName As String
        Do While reader.Read
            Select Case reader.NodeType
            Case XmlNodeType.Element
                 Console.WriteLine("XmlNodeType.Element " & reader.Name )
            Case XmlNodeType.Text
                 Console.WriteLine("XmlNodeType.Text " & reader.Value )
            Case XmlNodeType.EndElement
                 Console.WriteLine("XmlNodeType.EndElement " & reader.Name )
            End Select
        Loop
    End Sub
End Class

"<?xml version = "1.0"?>
"<books xmlns = "x-schema:book.xdr">
"   <book>
"      <title>C# How to Program</title>
"   </book>
"   <book>
"      <title>Java How to Program, 4/e</title>
"   </book>
"   <book>
"      <title>Visual Basic .NET How to Program</title>
"   </book>
"   <book>
"      <title>Advanced Java 2 Platform How to Program</title>
"   </book>
"   <book>
"      <title>Python How to Program</title>
"   </book>
"</books>