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())
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.