Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

DisplayXmlFile - loading a XML file in a TreeView
[ All Languages » VB.net »  XML]

Total Hit ( 2756)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
' Display a XML file in a TreeView
' Note: requires Imports System.Xml
' Example: DisplayXmlFile("employees.xml", TreeView1)

Sub DisplayXmlFile(ByVal filename As String, ByVal tvw As TreeView)
  Dim xmldoc As New XmlDocument()
  xmldoc.Load(filename)
  ' Add it to the TreeView Nodes collection
  DisplayXmlNode(xmldoc, tvw.Nodes)
  ' Expand the root node.
  tvw.Nodes(0).Expand()
End Sub

Sub DisplayXmlNode(ByVal xmlnode As XmlNode, ByVal nodes As TreeNodeCollection)
  ' Add a TreeView node for this XmlNode.
  ' (Using the node's Name is OK for most XmlNode types.)
  Dim tvNode As TreeNode = nodes.Add(xmlnode.Name)

  Select Case xmlnode.NodeType
    Case XmlNodeType.Element
      ' This is an element: Check whether there are attributes.
      If xmlnode.Attributes.Count > 0 Then
        ' Create an ATTRIBUTES node.
        Dim attrNode As TreeNode = tvNode.Nodes.Add("(ATTRIBUTES)")
        ' Add all the attributes as children of the new node.
        Dim xmlAttr As XmlAttribute
        For Each xmlAttr In xmlnode.Attributes
          ' Each node shows name and value.
          attrNode.Nodes.Add(xmlAttr.Name & " = '" & xmlAttr.Value & _
            "'")
        Next
      End If
    Case XmlNodeType.Text, XmlNodeType.CDATA
      ' For these node types we display the value
      tvNode.Text = xmlnode.Value
    Case XmlNodeType.Comment
      tvNode.Text = "<!--" & xmlnode.Value & "-->"
    Case XmlNodeType.ProcessingInstruction, XmlNodeType.XmlDeclaration
      tvNode.Text = "<?" & xmlnode.Name & " " & xmlnode.Value & "?>"
    Case Else
      ' ignore other node types.
  End Select

  ' Call this routine recursively for each child node.
  Dim xmlChild As XmlNode = xmlnode.FirstChild
  Do Until xmlChild Is Nothing
    DisplayXmlNode(xmlChild, tvNode.Nodes)
    xmlChild = xmlChild.NextSibling
  Loop
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.