Display XML Elements, no. of occurrence and its attributes

Sakthi.Singaravel
Posted by Sakthi.Singaravel under VB.NET category on | Points: 40 | Views : 1371
Hi,

Here the code for Display XML Elements, no. of occurrence and its attributes:

Imports System.Xml
Imports System.IO
Public Class Form1
Dim fname As String
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox3.Items.Clear()
Try
Dim tr As XmlTextReader = New XmlTextReader(TextBox1.Text)
While tr.Read()
If tr.Name = ListBox1.Text Then
If tr.HasAttributes Then
While tr.MoveToNextAttribute()
If ListBox3.Items.Contains(tr.Name) Then
Else
ListBox3.Items.Add(tr.Name)
End If
End While
End If
End If
End While
If ListBox3.Items.Count = 0 Then
ListBox3.Items.Add("No Attributes of this Element")
End If
Catch ex As FileNotFoundException
MsgBox(ex.ToString())
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exit_Btn.Click
End
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Clear()
ListBox2.Items.Clear()
ListBox3.Items.Clear()
TextBox1.Text = ""
OpenFileDialog1.FileName = "Open a XML file..."
OpenFileDialog1.Filter = "XML Files (*.xml)|*.xml"
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
fname = OpenFileDialog1.FileName.ToString()
End Sub

Private Sub Execute_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Execute_Btn.Click
Dim loc As String = Trim(TextBox1.Text)
Dim doc As New XmlDocument
Try
doc.Load(loc)
Label1.Text = "Document Element : " + doc.DocumentElement.Name()
Dim tr As XmlTextReader = New XmlTextReader(loc)
Dim wr As New StreamWriter("D:\Singaravel M\VB .net\log_element_count.txt", True)
wr.WriteLine()
wr.WriteLine(Now.ToShortDateString() + " " + Now.ToShortTimeString())
wr.WriteLine(TextBox1.Text)
wr.WriteLine("Root Element : " + doc.DocumentElement.Name())
While tr.Read()
If tr.NodeType = XmlNodeType.Element Then
If ListBox1.Items.Contains(tr.Name.ToString()) Or (doc.DocumentElement.Name() = tr.Name.ToString()) Then
Else
ListBox1.Items.Add(tr.Name.ToString())
ListBox2.Items.Add(doc.GetElementsByTagName(tr.Name.ToString()).Count.ToString())
wr.Write(tr.Name.ToString() + " ")
wr.WriteLine(doc.GetElementsByTagName(tr.Name.ToString()).Count.ToString() + " Time(s)")
End If
End If
End While
wr.Close()
Catch ex As FileNotFoundException
MsgBox("File NOT Found. Select Proper XML File.", MsgBoxStyle.Information, "File NOT Found")
Catch ex As ArgumentException
MsgBox("First Select the file, Using ""Browse"" Button", MsgBoxStyle.Information, "Select file")
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
End Class

Comments or Responses

Login to post response