October 3, 2013 | Posted in: .Net

Imports System.Drawing
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports System.Xml

 

Public NotInheritable Class XMLViewer

Public Shared Sub ViewXML(ByVal parentForm As Form, ByVal xml As String)

If ((parentForm IsNot Nothing) AndAlso (Not String.IsNullOrEmpty(xml))) Then

Dim xmlDisplay As New System.Windows.Forms.Form
Dim brokerXML As New RichTextBox()
Dim output As StringBuilder = New StringBuilder()
Dim ws As XmlWriterSettings = New XmlWriterSettings()

Dim formSize As New Size(CInt(parentForm.Width / 2), (parentForm.Height – CInt(parentForm.Height / 6)))

brokerXML.Multiline = True
brokerXML.Dock = DockStyle.Fill

xmlDisplay.Size = formSize
xmlDisplay.MinimumSize = formSize
xmlDisplay.Text = “XML Viewer”
xmlDisplay.StartPosition = FormStartPosition.CenterParent
xmlDisplay.Controls.Add(brokerXML)

ws.Indent = True

Try
Using reader As XmlReader = XmlReader.Create(New StringReader(xml))

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

brokerXML.Text = output.ToString()

xmlDisplay.ShowDialog()

Catch
Throw
Finally
If (xml IsNot Nothing) Then
xml = Nothing
End If

If (brokerXML IsNot Nothing) Then
brokerXML.Dispose()
End If

If (output IsNot Nothing) Then
output = Nothing
End If

If (ws IsNot Nothing) Then
ws = Nothing
End If

If (xmlDisplay IsNot Nothing) Then
xmlDisplay.Dispose()
xmlDisplay = Nothing
End If
End Try

End If

End Sub

End Class

Be the first to comment.

Leave a Reply

*


7 + three =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>