Results 1 to 4 of 4

Thread: xml

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    xml

    Could you let me know how to loop through an xml file and retrieve the values of the elements and the attributes.
    Thanks

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, do you want returned as a string?

    VB Code:
    1. Dim sb As New System.Text.StringBuilder
    2.         Dim fs As New System.IO.FileStream("c:\myxml.xml", IO.FileMode.Open)
    3.         Dim r As New System.Xml.XmlTextReader(fs)
    4.         While r.Read
    5.             sb.Append(r.ReadString)
    6.         End While
    7.         r.Close()
    8.         fs.Close()
    9.  
    10.         Debug.WriteLine(sb.ToString)

    That will give you just the data in the nodes... not the identifying attributes or markup.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    this will get you the file copntents
    VB Code:
    1. Dim sb As New System.Text.StringBuilder
    2.         Dim fs As New System.IO.FileStream("c:\myxml.xml", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    3.  
    4.         Dim mybytes(fs.Length) As Byte
    5.         fs.Read(mybytes, 0, mybytes.Length)
    6.         fs.Close()
    7.  
    8.         Dim myEnc As New System.Text.UnicodeEncoding
    9.         'byte > string
    10.         Dim s As String = myEnc.GetString(mybytes)
    11.         Debug.WriteLine(s.ToString)

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can also use the XMLDocument object:
    VB Code:
    1. Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
    2.         'load doc
    3.         Dim xdoc As New Xml.XmlDocument
    4.         xdoc.Load("data.xml")
    5.  
    6.         'get something to hold the values
    7.         Dim sb As New System.Text.StringBuilder
    8.         'loop through children and call recursive function to get values
    9.         For Each node As Xml.XmlNode In xdoc.ChildNodes
    10.             If node.NodeType = Xml.XmlNodeType.Element Then
    11.                 sb.AppendFormat("{0}{1}", RecursiveGetText(node), ControlChars.NewLine)
    12.             End If
    13.         Next
    14.  
    15.         'display result
    16.         txtXML.Text = sb.ToString
    17.     End Sub
    18.  
    19.     Private Function RecursiveGetText(ByVal node As Xml.XmlNode) As String
    20.         Dim sb As New System.Text.StringBuilder
    21.         'write current node values
    22.         sb.AppendFormat("{0}:{1}", node.Name, node.InnerText)
    23.         If Not node.Attributes Is Nothing Then
    24.             'write attributes
    25.             For Each atr As Xml.XmlAttribute In node.Attributes
    26.                 sb.AppendFormat(" @{0}:{1}", atr.Name, atr.Value)
    27.             Next
    28.         End If
    29.         sb.Append(ControlChars.NewLine)
    30.         'call recursively to get all generations
    31.         For Each child As Xml.XmlNode In node.ChildNodes
    32.             If child.NodeType = Xml.XmlNodeType.Element Then
    33.                 sb.Append(RecursiveGetText(child))
    34.             End If
    35.         Next
    36.  
    37.         Return sb.ToString
    38.     End Function

    It just depends on what you want to do with the values or how you want to get them. There are lots of choices.

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