Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / XML

XML Editor

Rate me:
Please Sign up or sign in to vote.
4.63/5 (9 votes)
25 Oct 20052 min read 106K   6.1K   72  
XML Editor with intellisense, automatic tag completion and parsing etc...
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Xml
Imports System.Xml.Schema

Public Class Editor
    Inherits System.Windows.Forms.Form

#Region " Developer information "

    'Developed by Kannan K.R.
    'Email : kannan.k.ram@gmail.com
    '
    'Feel free to write your suggestions...


#End Region


#Region " Declare variables here "


    Dim TempFile As String
    Dim Changed As Boolean = False
    Dim TagStack As New Stack()
    Dim DtdDt As DataTable

    Dim IsValid As Boolean
    Dim ErrStr As String
    Dim lineInf As IXmlLineInfo




#End Region


#Region " My Modules "


    Private Sub OpenFile()

        OFD1.Filter = "XML Files (*.xml)|*.xml|Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        OFD1.FilterIndex = 1
        OFD1.Title = "Select a file to open"
        OFD1.FileName = ""
        OFD1.ShowDialog()

        If OFD1.FileName <> "" Then

            TempFile = OFD1.FileName
            RichTextBox1.LoadFile(OFD1.FileName, RichTextBoxStreamType.PlainText)
            Me.Text = "Xml Editor | " + TempFile.Substring(TempFile.LastIndexOf("\") + 1)

        End If

        Changed = False

    End Sub



    Private Sub SaveFile()

        SFD1.Filter = "XML file (*.xml)|*.xml|Text file (*.txt)|*.txt|All files (*.*)|*.*"

        If TempFile.EndsWith(".xml") Then
            SFD1.FilterIndex = 1
        ElseIf TempFile.EndsWith(".txt") Then
            SFD1.FilterIndex = 2
        Else
            SFD1.FilterIndex = 3
        End If

        SFD1.FileName = Path.GetFileName(TempFile)

        SFD1.RestoreDirectory = True


        If SFD1.ShowDialog() = DialogResult.OK Then

            If SFD1.FileName <> "" Then
                RichTextBox1.SaveFile(SFD1.FileName, RichTextBoxStreamType.PlainText)
                TempFile = SFD1.FileName
                Me.Text = "Xml Editor | " + Path.GetFileName(TempFile)

            End If

        End If

        Changed = False

    End Sub



    Private Sub CloseOrNew()

        If Changed = True Then
            Dim DResult As DialogResult
            DResult = MsgBox("Do you want to save changes?", MsgBoxStyle.YesNoCancel, "Save")
            If DResult = MsgBoxResult.Yes Then


                If Me.Text = "Xml Editor | Untitled.xml" Then
                    SaveFile()
                Else
                    RichTextBox1.SaveFile(TempFile, RichTextBoxStreamType.PlainText)
                    Changed = False
                End If

                TempFile = Path.GetTempPath + "Untitled.xml"
                Me.Text = "Xml Editor | Untitled.xml"
                RichTextBox1.Clear()
                Changed = False


            ElseIf DResult = DialogResult.No Then

                TempFile = Path.GetTempPath + "Untitled.xml"
                Me.Text = "Xml Editor | Untitled.xml"
                RichTextBox1.Clear()
                Changed = False

            Else


            End If

        Else

            TempFile = Path.GetTempPath + "Untitled.xml"
            Me.Text = "Xml Editor | Untitled.xml"
            RichTextBox1.Clear()
            Changed = False


        End If

    End Sub




    Private Function ConstructTag(ByVal ElementNameParam As String) As String


        Dim ElementName As String
        ElementName = ElementNameParam
        Dim myCol As DataColumn
        Dim myRow As DataRow

        Try


            Dim currRows() As DataRow = DtdDt.Select(Nothing, Nothing, DataViewRowState.CurrentRows)

            If (currRows.Length < 1) Then
                RichTextBox1.Text += "No Current Rows Found"
            Else

                For Each myRow In currRows
                    If myRow(2).ToString = "Att" Then

                        If myRow(0).ToString = ElementNameParam.Trim Then
                            ElementName = ElementName + " " + myRow(1).ToString + "="" """
                        End If
                    End If

                Next
            End If

            ElementName += ">"


        Catch Ex As Exception


        End Try



        Return ElementName
    End Function




    Private Sub DTDReader(ByVal DTDName As String)

        'This module reads a DTD and add its Elements to a ComboBox and 
        'Attributes into a DataTable.

        'Once user selects a Tag from ComboBox, it searchs corresponding
        'attributes in DataTable


        Dim DtdStrRd As New StreamReader(DTDName)
        Dim m As Match
        Dim RowX As DataRow

        Dim DtdFileContents, ArrOfLines(), OneLine, ElementName, ElementOfAtts, ArrOfAtts(), OneAttr, AllAtts As String

        ComboBox1.Items.Clear()

        DtdDt = New DataTable("DTD")

        DtdDt.Columns.Add("Elements")
        DtdDt.Columns.Add("Attrs")
        DtdDt.Columns.Add("Type")





        DtdFileContents = DtdStrRd.ReadToEnd


        DtdFileContents = DtdFileContents.Replace(vbNewLine, " ")
        DtdFileContents = DtdFileContents.Replace(vbTab, " ")
        DtdFileContents = DtdFileContents.Replace(vbCr, " ")
        DtdFileContents = DtdFileContents.Replace(vbLf, " ")
        DtdFileContents = DtdFileContents.Replace(vbCrLf, " ")

        DtdFileContents = DtdFileContents.Replace(">", ">" + vbNewLine)


        ArrOfLines = DtdFileContents.Split(vbNewLine)





        For Each OneLine In ArrOfLines



            If OneLine <> "" Then
                OneLine = Trim(OneLine)
                m = Regex.Match(OneLine, "<!ELEMENT ([^ ]+)", RegexOptions.IgnoreCase)
                If m.Success Then


                    ElementName = Regex.Replace(OneLine, "([^<]*)<!ELEMENT ([^ ]+)(.*)", "$2", RegexOptions.Multiline)


                    'Add Elements to ComboBox
                    ComboBox1.Items.Add(ElementName)

                    RowX = DtdDt.NewRow

                    RowX(0) = ElementName.Replace(vbNewLine, "")
                    RowX(1) = " "
                    RowX(2) = "Elm"

                    'Add Element name to DataTable
                    DtdDt.Rows.Add(RowX)
                    RowX = Nothing
                Else
                    m = Regex.Match(OneLine, "<!ATTLIST ([^ ]+)", RegexOptions.IgnoreCase)

                    If m.Success Then
                        OneLine = OneLine.Replace(" NMTOKEN", "")
                        OneLine = OneLine.Replace(" #REQUIRED", "")
                        OneLine = OneLine.Replace(" #IMPLIED", "")
                        OneLine = OneLine.Replace(" IDS", "")
                        OneLine = OneLine.Replace(" ID", "")
                        OneLine = OneLine.Replace(" #FIXED", "")
                        OneLine = OneLine.Replace(" CDATA", "")
                        OneLine = OneLine.Replace(" NOTATION", "")

                        OneLine = OneLine.Replace(vbNewLine, "")
                        OneLine = OneLine.Replace(vbCrLf, "")
                        OneLine = OneLine.Replace(vbLf, "")
                        OneLine = OneLine.Replace(vbCr, "")


                        OneLine = Regex.Replace(OneLine, "(.*) \((.*)\)(.*)", "$1$3")
                        ElementOfAtts = Regex.Replace(OneLine, "(.*)<!ATTLIST ([^ ]+)(.*)", "$2")
                        AllAtts = Regex.Replace(OneLine, "(.*)\<!ATTLIST ([^ ]+) (.*)\>", "$3")



                        ArrOfAtts = AllAtts.Split(" ")

                        For Each OneAttr In ArrOfAtts

                            OneAttr = Regex.Replace(OneAttr, "([0-9A-Za-z_]+)", "$1").ToString

                            RowX = DtdDt.NewRow


                            RowX(0) = ElementOfAtts
                            RowX(1) = OneAttr
                            RowX(2) = "Att"

                            'Add Attributes to DataTable
                            DtdDt.Rows.Add(RowX)
                            RowX = Nothing
                        Next

                    End If


                End If

            End If


        Next


        ComboBox1.Items.Add("/")  'for tag closing
        ComboBox1.Items.Add("?")  'for xml declaration
        ComboBox1.Items.Add("!")  'for DTD declaration




    End Sub



    Private Sub IndentFormat()


        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

        RichTextBox1.SaveFile(TempFile, RichTextBoxStreamType.PlainText)

        Dim IfErr As Boolean = False
        Dim StrR As New StreamReader(TempFile)
        Dim StrW As New StreamWriter(Path.GetTempPath + "kan.tmp", False)
        Dim AllData As String = StrR.ReadToEnd
        Dim m As Match

        Dim TagS, TagE, TagM, RestD As String, i As Integer

        'Converting entire file to a single line

        AllData = AllData.Replace(vbNewLine, "")
        AllData = AllData.Replace(vbCrLf, "")
        AllData = AllData.Replace(vbLf, "")
        AllData = AllData.Replace(vbCr, "")
        AllData = AllData.Replace(vbTab, "").Trim



        'Looking for Processing Instruction and DTD declaration

        For i = 0 To 3 'We assume only first 4 lines have Processing Instruction and DTD declaration
            m = Regex.Match(AllData, "^\<\?([^>]+)\>", RegexOptions.IgnoreCase) 'go to MSDN for RegularExpression Help
            TagS = ""
            If m.Success Then
                TagS = Regex.Replace(AllData, "^\<\?([^>]+)\>(.*)", "<?$1>", RegexOptions.IgnoreCase)
                AllData = Regex.Replace(AllData, "^\<\?([^>]+)\>(.*)", "$2", RegexOptions.IgnoreCase)
                StrW.WriteLine(TagS)
            Else
                m = Regex.Match(AllData, "^\<\!DOCTYPE([^>]+)\>", RegexOptions.IgnoreCase)
                If m.Success Then
                    TagS = Regex.Replace(AllData, "^\<\!DOCTYPE([^>]+)\>(.*)", "<!DOCTYPE$1>", RegexOptions.IgnoreCase)
                    AllData = Regex.Replace(AllData, "^\<\!DOCTYPE([^>]+)\>(.*)", "$2", RegexOptions.IgnoreCase)
                    StrW.WriteLine(TagS)
                End If

            End If



        Next

        Dim LevelX, j As Integer, TabC As String




        Do
            TagS = ""
            TabC = ""
            m = Regex.Match(AllData, "^\<([^>/]+)\>([^<]+)\<\/([^>/]+)\>") 'Opening Tag
            If m.Success Then
                TagS = Regex.Replace(AllData, "^\<([^>/]+)\>([^<]+)\<\/([^>/]+)\>(.*)", "<$1>$2</$3>")

                AllData = Regex.Replace(AllData, "^\<([^>/]+)\>([^<]+)\<\/([^>/]+)\>(.*)", "$4")

                For j = 1 To LevelX 'Calculating depth of tag
                    TabC += vbTab
                Next
                StrW.Write(TabC)
                StrW.WriteLine(TagS)
            Else
                m = Regex.Match(AllData, "^\<\/([^>]+)\>(.*)") 'Closing Tag
                If m.Success Then

                    TagS = Regex.Replace(AllData, "^\<\/([^>]+)\>(.*)", "</$1>")
                    AllData = Regex.Replace(AllData, "^\<\/([^>]+)\>(.*)", "$2")
                    LevelX -= 1

                    For j = 1 To LevelX
                        TabC += vbTab
                    Next

                    StrW.Write(TabC)
                    StrW.WriteLine(TagS)

                Else
                    m = Regex.Match(AllData, "^\<([^>]+)\>(.*)")
                    If m.Success Then
                        TagS = Regex.Replace(AllData, "^\<([^>]+)\>(.*)", "<$1>")
                        AllData = Regex.Replace(AllData, "^\<([^>]+)\>(.*)", "$2")
                        LevelX += 1
                        For j = 1 To LevelX - 1
                            TabC += vbTab
                        Next
                        StrW.Write(TabC)

                        StrW.WriteLine(TagS)

                    Else
                        m = Regex.Match(AllData, "^([^<]+)\<")
                        If m.Success Then
                            TagS = Regex.Replace(AllData, "^([^<]+)\<(.*)", "$1")

                            AllData = Regex.Replace(AllData, "^([^<]+)\<(.*)", "<$2")

                            For j = 0 To LevelX - 1
                                TabC += vbTab
                            Next
                            StrW.Write(TabC)

                            StrW.WriteLine(TagS)

                        Else
                            MsgBox("This is not a proper XML document", MsgBoxStyle.Information)
                            IfErr = True
                            Exit Do

                        End If

                    End If

                End If


            End If


            If AllData.Length < 2 Then
                Exit Do
            End If

        Loop While True






        StrR.Close()
        StrW.Close()

        If IfErr = False Then
            RichTextBox1.LoadFile(Path.GetTempPath + "kan.tmp", RichTextBoxStreamType.PlainText)
        End If

        Me.Cursor = System.Windows.Forms.Cursors.Default

    End Sub



    Private Sub ParseFile()


        If TempFile Is Nothing Then
            MsgBox("Please open an XML file for parsing", MsgBoxStyle.Information, "Error")
            Exit Sub
        End If

        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor


        RichTextBox1.SaveFile(TempFile, RichTextBoxStreamType.PlainText)

        Dim xmlP As New XmlTextReader(TempFile)
        Dim xmlV As New XmlValidatingReader(xmlP)
        ErrStr = ""
        ListBox1.Items.Clear()



        AddHandler xmlV.ValidationEventHandler, AddressOf WriteErrorLog



        IsValid = True


        Do
            Try
                If xmlV.Read() Then
                    lineInf = CType(xmlV, IXmlLineInfo)
                End If


            Catch exx As Exception

                Try

                    IsValid = False

                    If lineInf.HasLineInfo Then
                        ErrStr = lineInf.LineNumber.ToString + ": " + lineInf.LinePosition.ToString + " " + exx.Message
                    End If

                    If exx.Message.IndexOf("EndElement") > 1 Then
                        Exit Do
                    End If

                    ListBox1.Items.Add(ErrStr)

                Catch eeex As Exception
                    MsgBox("Some unexpected error occurred " + vbNewLine + eeex.Message, MsgBoxStyle.Information, "Error")
                    Exit Do
                End Try

            End Try


        Loop While Not xmlP.EOF

        xmlV.Close()
        xmlP.Close()

        Me.Cursor = System.Windows.Forms.Cursors.Default


        If IsValid = False Then
            MsgBox("File is not valid", MsgBoxStyle.Exclamation, "Error")
        Else
            MsgBox("File is valid", MsgBoxStyle.Information, "OK")
        End If



    End Sub

    Private Sub WriteErrorLog(ByVal sender As Object, ByVal args As ValidationEventArgs)

        IsValid = False
        ErrStr = lineInf.LineNumber.ToString + ": " + lineInf.LinePosition.ToString + " " + args.Message
        ListBox1.Items.Add(ErrStr)

    End Sub





#End Region


#Region " Windows Form Designer generated code "



    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem10 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem11 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem12 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem13 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem14 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem15 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem16 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem17 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem18 As System.Windows.Forms.MenuItem
    Friend WithEvents OFD1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents SFD1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents MenuItem19 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem20 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem21 As System.Windows.Forms.MenuItem
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Panel2 As System.Windows.Forms.Panel
    Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
    Friend WithEvents Panel3 As System.Windows.Forms.Panel
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Editor))
        Me.MainMenu1 = New System.Windows.Forms.MainMenu()
        Me.MenuItem1 = New System.Windows.Forms.MenuItem()
        Me.MenuItem2 = New System.Windows.Forms.MenuItem()
        Me.MenuItem3 = New System.Windows.Forms.MenuItem()
        Me.MenuItem4 = New System.Windows.Forms.MenuItem()
        Me.MenuItem5 = New System.Windows.Forms.MenuItem()
        Me.MenuItem6 = New System.Windows.Forms.MenuItem()
        Me.MenuItem7 = New System.Windows.Forms.MenuItem()
        Me.MenuItem8 = New System.Windows.Forms.MenuItem()
        Me.MenuItem9 = New System.Windows.Forms.MenuItem()
        Me.MenuItem10 = New System.Windows.Forms.MenuItem()
        Me.MenuItem11 = New System.Windows.Forms.MenuItem()
        Me.MenuItem12 = New System.Windows.Forms.MenuItem()
        Me.MenuItem13 = New System.Windows.Forms.MenuItem()
        Me.MenuItem14 = New System.Windows.Forms.MenuItem()
        Me.MenuItem15 = New System.Windows.Forms.MenuItem()
        Me.MenuItem16 = New System.Windows.Forms.MenuItem()
        Me.MenuItem19 = New System.Windows.Forms.MenuItem()
        Me.MenuItem20 = New System.Windows.Forms.MenuItem()
        Me.MenuItem21 = New System.Windows.Forms.MenuItem()
        Me.MenuItem17 = New System.Windows.Forms.MenuItem()
        Me.MenuItem18 = New System.Windows.Forms.MenuItem()
        Me.OFD1 = New System.Windows.Forms.OpenFileDialog()
        Me.SFD1 = New System.Windows.Forms.SaveFileDialog()
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.Panel3 = New System.Windows.Forms.Panel()
        Me.TabControl1 = New System.Windows.Forms.TabControl()
        Me.TabPage1 = New System.Windows.Forms.TabPage()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox()
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()
        Me.TabPage2 = New System.Windows.Forms.TabPage()
        Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser()
        Me.Splitter1 = New System.Windows.Forms.Splitter()
        Me.Panel2 = New System.Windows.Forms.Panel()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.Panel1.SuspendLayout()
        Me.Panel3.SuspendLayout()
        Me.TabControl1.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.TabPage2.SuspendLayout()
        CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.Panel2.SuspendLayout()
        Me.SuspendLayout()
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem11, Me.MenuItem14, Me.MenuItem19, Me.MenuItem17})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem4, Me.MenuItem5, Me.MenuItem6, Me.MenuItem7, Me.MenuItem8, Me.MenuItem9, Me.MenuItem10})
        Me.MenuItem1.Text = "File"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 0
        Me.MenuItem2.Shortcut = System.Windows.Forms.Shortcut.CtrlN
        Me.MenuItem2.Text = "&New"
        '
        'MenuItem3
        '
        Me.MenuItem3.Index = 1
        Me.MenuItem3.Shortcut = System.Windows.Forms.Shortcut.CtrlO
        Me.MenuItem3.Text = "&Open"
        '
        'MenuItem4
        '
        Me.MenuItem4.Index = 2
        Me.MenuItem4.Shortcut = System.Windows.Forms.Shortcut.CtrlS
        Me.MenuItem4.Text = "&Save"
        '
        'MenuItem5
        '
        Me.MenuItem5.Index = 3
        Me.MenuItem5.Shortcut = System.Windows.Forms.Shortcut.CtrlShiftS
        Me.MenuItem5.Text = "Save &As"
        '
        'MenuItem6
        '
        Me.MenuItem6.Index = 4
        Me.MenuItem6.Shortcut = System.Windows.Forms.Shortcut.CtrlF4
        Me.MenuItem6.Text = "&Close"
        '
        'MenuItem7
        '
        Me.MenuItem7.Index = 5
        Me.MenuItem7.Text = "-"
        '
        'MenuItem8
        '
        Me.MenuItem8.Index = 6
        Me.MenuItem8.Shortcut = System.Windows.Forms.Shortcut.CtrlL
        Me.MenuItem8.Text = "Load &DTD"
        '
        'MenuItem9
        '
        Me.MenuItem9.Index = 7
        Me.MenuItem9.Text = "-"
        '
        'MenuItem10
        '
        Me.MenuItem10.Index = 8
        Me.MenuItem10.Shortcut = System.Windows.Forms.Shortcut.AltF4
        Me.MenuItem10.Text = "E&xit"
        '
        'MenuItem11
        '
        Me.MenuItem11.Index = 1
        Me.MenuItem11.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem12, Me.MenuItem13})
        Me.MenuItem11.Text = "&Insert"
        '
        'MenuItem12
        '
        Me.MenuItem12.Index = 0
        Me.MenuItem12.Text = "&DTD"
        '
        'MenuItem13
        '
        Me.MenuItem13.Index = 1
        Me.MenuItem13.Text = "&Stylesheet"
        '
        'MenuItem14
        '
        Me.MenuItem14.Index = 2
        Me.MenuItem14.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem15, Me.MenuItem16})
        Me.MenuItem14.Text = "&View"
        '
        'MenuItem15
        '
        Me.MenuItem15.Index = 0
        Me.MenuItem15.Text = "&Source"
        '
        'MenuItem16
        '
        Me.MenuItem16.Index = 1
        Me.MenuItem16.Text = "&Output"
        '
        'MenuItem19
        '
        Me.MenuItem19.Index = 3
        Me.MenuItem19.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem20, Me.MenuItem21})
        Me.MenuItem19.Text = "&Tools"
        '
        'MenuItem20
        '
        Me.MenuItem20.Index = 0
        Me.MenuItem20.Shortcut = System.Windows.Forms.Shortcut.CtrlShiftP
        Me.MenuItem20.Text = "&Parse"
        '
        'MenuItem21
        '
        Me.MenuItem21.Index = 1
        Me.MenuItem21.Text = "&Format / Indent"
        '
        'MenuItem17
        '
        Me.MenuItem17.Index = 4
        Me.MenuItem17.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem18})
        Me.MenuItem17.Text = "&Help"
        '
        'MenuItem18
        '
        Me.MenuItem18.Index = 0
        Me.MenuItem18.Text = "&About"
        '
        'SFD1
        '
        Me.SFD1.FileName = "doc1"
        '
        'Panel1
        '
        Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel3, Me.Splitter1, Me.Panel2})
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(688, 369)
        Me.Panel1.TabIndex = 0
        '
        'Panel3
        '
        Me.Panel3.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabControl1})
        Me.Panel3.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Panel3.Name = "Panel3"
        Me.Panel3.Size = New System.Drawing.Size(688, 265)
        Me.Panel3.TabIndex = 2
        '
        'TabControl1
        '
        Me.TabControl1.Alignment = System.Windows.Forms.TabAlignment.Bottom
        Me.TabControl1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabPage1, Me.TabPage2})
        Me.TabControl1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TabControl1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.Size = New System.Drawing.Size(688, 265)
        Me.TabControl1.TabIndex = 2
        '
        'TabPage1
        '
        Me.TabPage1.Controls.AddRange(New System.Windows.Forms.Control() {Me.ComboBox1, Me.RichTextBox1})
        Me.TabPage1.Location = New System.Drawing.Point(4, 4)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Size = New System.Drawing.Size(680, 236)
        Me.TabPage1.TabIndex = 0
        Me.TabPage1.Text = "Source"
        '
        'ComboBox1
        '
        Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.ComboBox1.Location = New System.Drawing.Point(160, 64)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(160, 24)
        Me.ComboBox1.TabIndex = 1
        Me.ComboBox1.Visible = False
        '
        'RichTextBox1
        '
        Me.RichTextBox1.AcceptsTab = True
        Me.RichTextBox1.DetectUrls = False
        Me.RichTextBox1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.RichTextBox1.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.Size = New System.Drawing.Size(680, 236)
        Me.RichTextBox1.TabIndex = 1
        Me.RichTextBox1.Text = ""
        '
        'TabPage2
        '
        Me.TabPage2.Controls.AddRange(New System.Windows.Forms.Control() {Me.AxWebBrowser1})
        Me.TabPage2.Location = New System.Drawing.Point(4, 4)
        Me.TabPage2.Name = "TabPage2"
        Me.TabPage2.Size = New System.Drawing.Size(680, 236)
        Me.TabPage2.TabIndex = 1
        Me.TabPage2.Text = "Output"
        Me.TabPage2.Visible = False
        '
        'AxWebBrowser1
        '
        Me.AxWebBrowser1.ContainingControl = Me
        Me.AxWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.AxWebBrowser1.Enabled = True
        Me.AxWebBrowser1.OcxState = CType(resources.GetObject("AxWebBrowser1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxWebBrowser1.Size = New System.Drawing.Size(680, 236)
        Me.AxWebBrowser1.TabIndex = 2
        '
        'Splitter1
        '
        Me.Splitter1.BackColor = System.Drawing.Color.IndianRed
        Me.Splitter1.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.Splitter1.Location = New System.Drawing.Point(0, 265)
        Me.Splitter1.Name = "Splitter1"
        Me.Splitter1.Size = New System.Drawing.Size(688, 4)
        Me.Splitter1.TabIndex = 1
        Me.Splitter1.TabStop = False
        '
        'Panel2
        '
        Me.Panel2.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1})
        Me.Panel2.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.Panel2.Location = New System.Drawing.Point(0, 269)
        Me.Panel2.Name = "Panel2"
        Me.Panel2.Size = New System.Drawing.Size(688, 100)
        Me.Panel2.TabIndex = 0
        '
        'ListBox1
        '
        Me.ListBox1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ListBox1.Font = New System.Drawing.Font("Verdana", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.ListBox1.HorizontalScrollbar = True
        Me.ListBox1.ItemHeight = 18
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(688, 94)
        Me.ListBox1.TabIndex = 0
        '
        'Editor
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(688, 369)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1})
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Menu = Me.MainMenu1
        Me.Name = "Editor"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Xml Editor | Untitled.xml"
        Me.Panel1.ResumeLayout(False)
        Me.Panel3.ResumeLayout(False)
        Me.TabControl1.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.TabPage2.ResumeLayout(False)
        CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.Panel2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region


#Region " Event handlers..."




    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click


        If Changed = True Then
            Dim DResult As DialogResult
            DResult = MsgBox("Do you want to save changes?", MsgBoxStyle.YesNoCancel, "Save")
            If DResult = MsgBoxResult.Yes Then


                If Me.Text = "Xml Editor | Untitled.xml" Then
                    SaveFile()
                Else
                    RichTextBox1.SaveFile(TempFile, RichTextBoxStreamType.PlainText)
                End If

                OpenFile()

            ElseIf DResult = DialogResult.No Then
                OpenFile()
            Else


            End If

        Else

            OpenFile()



        End If




    End Sub













    Private Sub MenuItem10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem10.Click

        Me.Close()

    End Sub


    Private Sub Editor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        TempFile = Path.GetTempPath + "Untitled.xml"
        Me.Text = "Xml Editor | Untitled.xml"

        MenuItem15.Checked = True
        MenuItem16.Checked = False

        ComboBox1.Items.Clear()
        ComboBox1.Items.Add("?")
        ComboBox1.Items.Add("!")




    End Sub








    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click

        If Me.Text = "Xml Editor | Untitled.xml" Then
            SaveFile()
        Else
            RichTextBox1.SaveFile(TempFile, RichTextBoxStreamType.PlainText)
            Changed = False
        End If

    End Sub

    Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
        SaveFile()
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

        If TabControl1.SelectedIndex = 1 Then
            RichTextBox1.SaveFile(TempFile, RichTextBoxStreamType.PlainText)
            AxWebBrowser1.Navigate(TempFile)

            MenuItem15.Checked = False
            MenuItem16.Checked = True
        Else
            MenuItem15.Checked = True
            MenuItem16.Checked = False

        End If



    End Sub

    Private Sub MenuItem15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem15.Click
        TabControl1.SelectedIndex = 0
    End Sub

    Private Sub MenuItem16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem16.Click
        TabControl1.SelectedIndex = 1
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Changed = True
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click, MenuItem6.Click
        CloseOrNew()
    End Sub



    Private Sub Editor_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Changed = True Then
            Dim DResult As DialogResult
            DResult = MsgBox("Do you want to save changes?", MsgBoxStyle.YesNoCancel, "Save")
            If DResult = MsgBoxResult.Yes Then


                If Me.Text = "Xml Editor | Untitled.xml" Then
                    SaveFile()
                Else
                    RichTextBox1.SaveFile(TempFile, RichTextBoxStreamType.PlainText)
                    Changed = False
                End If

                TempFile = Path.GetTempPath + "Untitled.xml"
                Me.Text = "Xml Editor | Untitled.xml"
                RichTextBox1.Clear()
                Changed = False


            ElseIf DResult = DialogResult.No Then

                TempFile = Path.GetTempPath + "Untitled.xml"
                Me.Text = "Xml Editor | Untitled.xml"
                RichTextBox1.Clear()
                Changed = False

            Else

                e.Cancel = True

            End If

        Else

            TempFile = Path.GetTempPath + "Untitled.xml"
            Me.Text = "Xml Editor | Untitled.xml"
            RichTextBox1.Clear()
            Changed = False


        End If


    End Sub

    Private Sub RichTextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress


        Dim LocationCombo As New Point()


        If e.KeyChar = "<" Then
            LocationCombo = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
            LocationCombo.Y = LocationCombo.Y + RichTextBox1.Font.Height + 3
            ComboBox1.Location = LocationCombo
            ComboBox1.Visible = True
            ComboBox1.DroppedDown = True
            ComboBox1.Focus()
        End If


    End Sub


    Private Sub ComboBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.Leave
        ComboBox1.Visible = False
        RichTextBox1.Focus()
    End Sub


    Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown

        If e.KeyCode = Keys.Enter Then


            If ComboBox1.Text = "?" Then
                RichTextBox1.SelectedText = "?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?>"
                ComboBox1.Visible = False
                RichTextBox1.Focus()

            ElseIf ComboBox1.Text = "!" Then
                RichTextBox1.SelectedText = "!DOCTYPE *** SYSTEM ""***.dtd"">"
                ComboBox1.Visible = False
                RichTextBox1.Focus()

            ElseIf ComboBox1.Text <> "/" Then
                TagStack.Push(ComboBox1.Text)

                RichTextBox1.SelectedText = ConstructTag(ComboBox1.Text)  'ComboBox1.Text + ">"
                ComboBox1.Visible = False
                RichTextBox1.Focus()

            Else

                If TagStack.Count > 0 Then
                    RichTextBox1.SelectedText = "/" + TagStack.Pop() + ">"
                    ComboBox1.Visible = False
                    RichTextBox1.Focus()
                Else
                    RichTextBox1.SelectedText = Keys.Back

                    ComboBox1.Visible = False
                    RichTextBox1.Focus()
                End If
            End If
        ElseIf e.KeyCode = Keys.Escape Then
            ComboBox1.Visible = False
            RichTextBox1.Focus()
        End If
    End Sub




    Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click

        OFD1.Filter = "DTD Files (*.dtd)|*.dtd|Tag Files (*.tag)|*.tag|All Files (*.*)|*.*"
        OFD1.FilterIndex = 1
        OFD1.Title = "Select a file"
        OFD1.FileName = ""
        OFD1.ShowDialog()

        If OFD1.FileName <> "" Then
            If OFD1.FileName.EndsWith(".dtd") Then
                DTDReader(OFD1.FileName)
            Else
                MsgBox("File is not a specified format", MsgBoxStyle.Information, "XML Editor")
            End If

        End If

    End Sub







    Private Sub MenuItem21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem21.Click
        IndentFormat()
    End Sub




    Private Sub MenuItem20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem20.Click
        ParseFile()
    End Sub


    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim SelItem As String
        Dim linN, colN As Integer
        SelItem = ListBox1.SelectedItem.ToString

        If SelItem <> "" Then

            linN = CType(Regex.Replace(SelItem, "([0-9]+): ([0-9]+)(.*)", "$1"), Integer)
            colN = CType(Regex.Replace(SelItem, "([0-9]+): ([0-9]+)(.*)", "$2"), Integer)


            Dim mc As MatchCollection
            Dim i As Integer = 0
            Dim totc As Integer

            mc = Regex.Matches(RichTextBox1.Text, "\n", RegexOptions.Singleline)

            Try
                RichTextBox1.Select(mc(linN - 2).Index + colN, 2)
                RichTextBox1.SelectionColor = Color.Blue
                RichTextBox1.Focus()

            Catch ex As Exception
                RichTextBox1.Focus()
            End Try
        End If


    End Sub

    Private Sub Editor_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        RichTextBox1.Focus()
    End Sub

#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
India India
A Software Engineer from Kerala, working in New Delhi, INDIA email: kannan.k.ram@gmail.com | kannankr.in

Comments and Discussions