Load an XML in a RichTextBox using C#

By Karthikeyan Anbarasan 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())
Karthikeyan Anbarasan

Karthikeyan Anbarasan (Karthik) Microsoft MVP in ASP.NET/IIS Architecture is a blogger & Founder of www.F5debug.net, also he is a speaker and Technology geek in Microsoft products. Karthik is working as a Technical C... Read more

Personal Blog: http://www.f5debug.net
COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs