Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27468

Not able to open Dialog Box to select file for editing xml

$
0
0
Need help with open file dialog box

What i am trying to achieve when i open the form is to be able to select an xml file for edditing and on clicking the nodes in the treeview, place the node attributes in the ListView, But I cant seem to open a windows dialog box to load my xml files, any help would be appreciated, also any pointers on modifying the eml file once i have it all loaded.

Attachment 93489

There is something wrong with my coding below, but not sure what
Code:

        If OpenFileDialog.ShowDialog() = DialogResult.Cancel Then
            Return
        End If

        FileNameTextBox.Text = OpenFileDialog.FileName
        XmlTreeView.Nodes.Clear()

        document = New XmlDocument()
        document.Load(fileDialogBox.FileName)


Code:

Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Xml
Imports System.Xml.XPath

Public Class Form1
    Private document As XmlDocument = Nothing

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

    Public Sub loadButton_Click(ByVal sender As Object, ByVal e As EventArgs)

        If OpenFileDialog.ShowDialog() = DialogResult.Cancel Then
            Return
        End If

        FileNameTextBox.Text = OpenFileDialog.FileName
        XmlTreeView.Nodes.Clear()

        document = New XmlDocument()
        document.Load(fileDialogBox.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

        XmlTreeView.ExpandAll()
    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)
        attributesListView.Items.Clear()

        If e.Node.Tag IsNot Nothing Then
            attributesListView.Items.AddRange(DirectCast(e.Node.Tag, ListViewItem()))
        End If
    End Sub
End Class

Attached Images
 

Viewing all articles
Browse latest Browse all 27468

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>