Results 1 to 7 of 7

Thread: If Else to remove comments and add node id

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2012
    Posts
    25

    If Else to remove comments and add node id

    The below code works and is used to display the Node address of a xml file to a treeview, but it will also pick up all comments within the xml because they also begin with <!-- and end with --> and also the xml header is seen <?xml version="1.0" encoding="utf8"?> is there any way i can avoid this ?

    Also i would like to add the nodes "id" if it contains one to the treeview, is this posible

    <Section6 Name="Not Used" id ="0,0006" Label="Full Description">
    Code:
        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
    Last edited by RoyLittle0; Nov 23rd, 2012 at 07:18 AM.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: If Else to remove comments and add node id

    Might we see the xml? It sounds distinctly odd from your description so it would be helpful to know exactly what we're trying to do.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2012
    Posts
    25

    Re: If Else to remove comments and add node id

    Odd is my middle name :0

    Code:
    <Root Name="ted" id="100" Tag="teddy">
    	<Root1 Name="Sam" id="101" Tag="Sammy">
    		<Root2 Name="Mary" id="102" Tag="Mandy">
    		</Root2>
    	</Root1>
    	<Root1 Name="Jam" id="103" Tag="Jammy">
    		<Root2 Name="Piggy" id="104" Tag="Randy">
    		</Root2>
    	</Root1>
    </Root>

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: If Else to remove comments and add node id

    I'm going to take back 'odd' and substitute 'weird'! I don't know that you wouldn't be better off abandoning the idea of treating this as xml and using it as a text file plain and simple using string methods and or Regex at first glance. I'll need to have a think!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2012
    Posts
    25

    Re: If Else to remove comments and add node id

    this part of the program displays the xml in a treeview and then on a click event sends the node with its 3 attributes to a listview where it will be edited and eventually saved back to the xml file.

    What i want is to see the node and its id in the treeview rater than <Root1> (there are 3071 nodes to each root) and there are 7 roots, making a total of 21497 nodes, this could be confusing without the id.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2012
    Posts
    25

    Re: If Else to remove comments and add node id

    The full code is below if that makes more sence

    Code:
    Imports System.Xml
    Imports System.IO
    
    Public Class Form6
        Private document As XmlDocument = Nothing
    
        Private Sub Button1_Click_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            document = New XmlDocument()
            document.Load("C:\VisionProject\IBS2000.xml")
    
            For Each node As XmlNode In document
                Dim treeNode As TreeNode = XmlNode2TreeNode(node)
                GetAttributes(node, treeNode)
                treeNode.Text = FormatName(node)
                TreeView1.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 TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
            AttributeListView.Items.Clear()
    
            If e.Node.Tag IsNot Nothing Then
                AttributeListView.Items.AddRange(DirectCast(e.Node.Tag, ListViewItem()))
            End If
        End Sub
    
    
        Private Sub AttributesListView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AttributeListView.Click
            With Me.AttributeListView
                Dim i As Integer
                For Each item As ListViewItem In AttributeListView.SelectedItems
                    i = item.Index
                Next
    
                Dim innercounter As Integer = 0
                For Each subItem As ListViewItem.ListViewSubItem In AttributeListView.Items(i).SubItems
                    Dim myString As String = AttributeListView.Items(i).SubItems(innercounter).Text
                    Select Case innercounter
                        Case 1
    
                    End Select
                    innercounter += 1
                Next
            End With
        End Sub
    
        Private Sub AttributesListView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AttributeListView.SelectedIndexChanged
            TextBox1.Text = AttributeListView.SelectedItems(0).SubItems(0).Text
            TextBox2.Text = AttributeListView.SelectedItems(1).SubItems(1).Text
            TextBox3.Text = AttributeListView.SelectedItems(2).SubItems(2).Text
        End Sub
    
    
        Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
    
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        End Sub
    
        Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    
        End Sub
    
        Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    
        End Sub
    
    End Class

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: If Else to remove comments and add node id

    I'm finding all that code and all those different display methods extremely confusing for what seems to be as simple as ...

    childTreeNode.Text = childNode.Attributes("id").Value
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width