RoyLittle0
Active member
OK, i'm sure you will all be horrified when you see my code, but i'm learning
This is as far as i have got, its not pretty but we all have to start somewhere and it is just a pet project, i can open up a file dialog box to select my xml and read it to the treeview, it then get the selections to the listview, when the listview selection is passed to a textbox for editing, i have a button ready for saving the file but just got to figure all that but out.
Be gentle with me i am trying to learn, all constructive comments welcome.
This is as far as i have got, its not pretty but we all have to start somewhere and it is just a pet project, i can open up a file dialog box to select my xml and read it to the treeview, it then get the selections to the listview, when the listview selection is passed to a textbox for editing, i have a button ready for saving the file but just got to figure all that but out.
Be gentle with me i am trying to learn, all constructive comments welcome.
VB.NET:
Imports System.Linq
Imports System.Xml
Imports System.Xml.XPath
Public Class Form1
Private document As XmlDocument = Nothing
Private Sub LoadButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
If OpenFileDialog.ShowDialog() = DialogResult.Cancel Then
Return
End If
FileNameTextBox.Text = OpenFileDialog.FileName
XmlTreeView.Nodes.Clear()
document = New XmlDocument()
document.Load(OpenFileDialog.FileName)
'load the XMl file
Dim nodeList As XmlNodeList = document.DocumentElement.ChildNodes
'loop through the top nodes
For Each node As XmlNode In document
Dim treeNode As TreeNode = XmlNode2TreeNode(node)
GetAttributes(node, treeNode)
treeNode.Text = FormatName(node)
XmlTreeView.Nodes.Add(treeNode)
Next
End Sub
Private Function XmlNode2TreeNode(ByVal parentNode As XmlNode) As TreeNode
Dim treeNode As New TreeNode()
For Each childNode As XmlNode In parentNode.ChildNodes
If childNode.NodeType = XmlNodeType.Element Then
Dim childTreeNode As New TreeNode()
If childNode.ChildNodes.Count > 0 Then
childTreeNode = XmlNode2TreeNode(childNode)
End If
childTreeNode.Text = FormatName(childNode)
GetAttributes(childNode, childTreeNode)
treeNode.Nodes.Add(childTreeNode)
End If
Next
Return treeNode
End Function
Private Function FormatName(ByVal node As XmlNode) As String
Dim fullName As String = "<" & node.Name
If node.InnerText <> "" Then
fullName += ">" & node.InnerText & "</" & node.Name & ">"
Else
fullName += ">"
End If
Return fullName
End Function
Private Sub GetAttributes(ByVal node As XmlNode, ByVal treeNode As TreeNode)
If node.Attributes IsNot Nothing Then
Dim attributes As ListViewItem() = New ListViewItem(node.Attributes.Count - 1) {}
For i As Integer = 0 To node.Attributes.Count - 1
attributes(i) = New ListViewItem(New String() {node.Attributes(i).Name, node.Attributes(i).Value})
Next
treeNode.Tag = attributes
End If
End Sub
Private Sub xmlTreeView_NodeMouseClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles XmlTreeView.NodeMouseClick
AttributesListView.Items.Clear()
If e.Node.Tag IsNot Nothing Then
AttributesListView.Items.AddRange(DirectCast(e.Node.Tag, ListViewItem()))
End If
End Sub
Private Sub OpenFileDialog_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog.FileOk
End Sub
Private Sub AttributesListView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AttributesListView.Click
With Me.AttributesListView
Dim i As Integer
For Each item As ListViewItem In AttributesListView.SelectedItems
i = item.Index
Next
Dim innercounter As Integer = 0
For Each subItem As ListViewItem.ListViewSubItem In AttributesListView.Items(i).SubItems
Dim myString As String = AttributesListView.Items(i).SubItems(innercounter).Text
Select Case innercounter
Case 1
LabelTextBox.Text = myString
End Select
innercounter += 1
Next
End With
End Sub
Private Sub SaveXmlButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveXmlButton.Click
End Sub
End Class