XmlReader Class represents a reader that provides fast, non-cached, forward-only access to XML data.
Imports System
Imports System.Text
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim output As StringBuilder = New StringBuilder()
Dim xmlString As String = "<?xml version='1.0'?>" & _
"<!-- This is a sample XML document -->" & _
"<Items>" & _
"<Item>test with a child element <more/> stuff</Item>" & _
"</Items>"
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
Dim ws As XmlWriterSettings = New XmlWriterSettings()
ws.Indent = True
Using writer As XmlWriter = XmlWriter.Create(output, ws)
While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
writer.WriteStartElement(reader.Name)
Case XmlNodeType.Text
writer.WriteString(reader.Value)
Case XmlNodeType.XmlDeclaration
Case XmlNodeType.ProcessingInstruction
writer.WriteProcessingInstruction(reader.Name, reader.Value)
Case XmlNodeType.Comment
writer.WriteComment(reader.Value)
Case XmlNodeType.EndElement
writer.WriteFullEndElement()
End Select
End While
End Using
End Using
Console.WriteLine(output.ToString())
End Sub
End Class
Related examples in the same category