Blog

Load an XML in a RichTextBox using C#

Posted by Karthikeyan Anbarasan Blogs | XML Mar 31, 2011
The code snippet in this blog shows how to load and display an XML document in a RichTextBox control using C#.
This blog shows on how to load an XML in a RichTextBox control.

Public Sub LoadXMLtoBOX(ByVal strXMLPath As String)

        Me.rtbTextbox1.Text = ""

        Dim reader As New XmlTextReader(strXMLPath)
        While reader.Read()
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    'The node is an element.
                    Me.rtbTextbox1.SelectionColor = Color.Blue
                    Me.rtbTextbox1.AppendText("<")
                    Me.rtbTextbox1.SelectionColor = Color.Brown
                    Me.rtbTextbox1.AppendText(reader.Name)
                    Me.rtbTextbox1.SelectionColor = Color.Blue
                    Me.rtbTextbox1.AppendText(">")
                    Exit Select
                Case XmlNodeType.Text
                    'Display the text in each element.
                    Me.rtbTextbox1.SelectionColor = Color.Black
                    Me.rtbTextbox1.AppendText(reader.Value)
                    Exit Select
                Case XmlNodeType.EndElement
                    'Display the end of the element.
                    Me.rtbTextbox1.SelectionColor = Color.Blue
                    Me.rtbTextbox1.AppendText("</")
                    Me.rtbTextbox1.SelectionColor = Color.Brown
                    Me.rtbTextbox1.AppendText(reader.Name)
                    Me.rtbTextbox1.SelectionColor = Color.Blue
                    Me.rtbTextbox1.AppendText(">")
                    Me.rtbTextbox1.AppendText(vbLf)
                    Exit Select
            End Select
        End While
        reader.Close()

    End Sub



    LoadXMLtoBOX(strXMLPath.ToString())
COMMENT USING
PREMIUM SPONSORS
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.