Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. XML Extraction using VB

XML Extraction using VB

Scheduled Pinned Locked Moved Visual Basic
xmljsonquestionannouncement
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nhsal69
    wrote on last edited by
    #1

    Hey there I'm having trouble getting one element out of this XML file.. <sizedata Size= "VALUE" IGNORE THE REST OF THEM/> here is a fully formed, but shortened XML file: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Root Type="TRoot"> <Date>15/10/2009 12:30:08</Date> <Folder fullpath="C:\" IsFilesNode="0"> <Name>C:\</Name> <SizeData Size="74609238013" Allocated="72396726232" Wasted="234062016" CDRom="74727184384" Files="116733" Folders="12272" Compression="3"/> </Folder> <Folder fullpath="C:\" IsFilesNode="-1"> <Name>[Files]</Name> <SizeData Size="18745407271" Allocated="18745561088" Wasted="153817" CDRom="18745475072" Files="69" Folders="0" Compression="1"/> </Folder> <Folder fullpath="C:\TEMP_\" IsFilesNode="0"> <Name>TEMP_ALAN</Name> <SizeData Size="15469174140" Allocated="15489126222" Wasted="19886724" CDRom="15478267904" Files="9570" Folders="1832" Compression="1"/> </Folder> <Folder fullpath="C:\TEMP_\mp3\" IsFilesNode="0"> <Name>mp3</Name> <SizeData Size="11504514137" Allocated="11513361814" Wasted="8829863" CDRom="11508457472" Files="4561" Folders="510" Compression="1"/> </Folder> <Folder fullpath="C:\TEMP_\mp3\My Music\" IsFilesNode="0"> <Name>My Music</Name> <SizeData Size="11247143252" Allocated="11255940398" Wasted="8779436" CDRom="11251054592" Files="4532" Folders="506" Compression="1"/> </Folder> <Folder fullpath="C:\TEMP_\mp3\My Music\MP3\" IsFilesNode="0"> <Name>MP3</Name> <SizeData Size="11074836494" Allocated="11083256268" Wasted="8402930" CDRom="11078576128" Files="4357" Folders="481" Compression="1"/> </Folder> <Folder fullpath="C:\TEMP_\mp3\My Music\MP3\MP3\" IsFilesNode="0"> <Name>MP3</Name> <SizeData Size="9808652194" Allocated="9813168460" Wasted="4507742" CDRom="9810542592" Files="2458" Folders="242" Compression="1"/> </Folder> </Root> The code I have is below: <code>Try    Dim odoc As New System.Xml.XmlDocument    odoc.Load("C:\test\test.xml")    Dim oXmlLog As System.Xml.XmlElement    Dim text As String = ""    For Each oXmlLog In odoc.SelectNodes("Root")          Dim node As System.Xml.XmlElement             For Each node In oXmlLog.SelectNodes("Fold

    C 1 Reply Last reply
    0
    • N nhsal69

      Hey there I'm having trouble getting one element out of this XML file.. <sizedata Size= "VALUE" IGNORE THE REST OF THEM/> here is a fully formed, but shortened XML file: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Root Type="TRoot"> <Date>15/10/2009 12:30:08</Date> <Folder fullpath="C:\" IsFilesNode="0"> <Name>C:\</Name> <SizeData Size="74609238013" Allocated="72396726232" Wasted="234062016" CDRom="74727184384" Files="116733" Folders="12272" Compression="3"/> </Folder> <Folder fullpath="C:\" IsFilesNode="-1"> <Name>[Files]</Name> <SizeData Size="18745407271" Allocated="18745561088" Wasted="153817" CDRom="18745475072" Files="69" Folders="0" Compression="1"/> </Folder> <Folder fullpath="C:\TEMP_\" IsFilesNode="0"> <Name>TEMP_ALAN</Name> <SizeData Size="15469174140" Allocated="15489126222" Wasted="19886724" CDRom="15478267904" Files="9570" Folders="1832" Compression="1"/> </Folder> <Folder fullpath="C:\TEMP_\mp3\" IsFilesNode="0"> <Name>mp3</Name> <SizeData Size="11504514137" Allocated="11513361814" Wasted="8829863" CDRom="11508457472" Files="4561" Folders="510" Compression="1"/> </Folder> <Folder fullpath="C:\TEMP_\mp3\My Music\" IsFilesNode="0"> <Name>My Music</Name> <SizeData Size="11247143252" Allocated="11255940398" Wasted="8779436" CDRom="11251054592" Files="4532" Folders="506" Compression="1"/> </Folder> <Folder fullpath="C:\TEMP_\mp3\My Music\MP3\" IsFilesNode="0"> <Name>MP3</Name> <SizeData Size="11074836494" Allocated="11083256268" Wasted="8402930" CDRom="11078576128" Files="4357" Folders="481" Compression="1"/> </Folder> <Folder fullpath="C:\TEMP_\mp3\My Music\MP3\MP3\" IsFilesNode="0"> <Name>MP3</Name> <SizeData Size="9808652194" Allocated="9813168460" Wasted="4507742" CDRom="9810542592" Files="2458" Folders="242" Compression="1"/> </Folder> </Root> The code I have is below: <code>Try    Dim odoc As New System.Xml.XmlDocument    odoc.Load("C:\test\test.xml")    Dim oXmlLog As System.Xml.XmlElement    Dim text As String = ""    For Each oXmlLog In odoc.SelectNodes("Root")          Dim node As System.Xml.XmlElement             For Each node In oXmlLog.SelectNodes("Fold

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      InnerText will give you all the inner nodes as well. Use .Value instead.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      N 1 Reply Last reply
      0
      • C Christian Graus

        InnerText will give you all the inner nodes as well. Use .Value instead.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        N Offline
        N Offline
        nhsal69
        wrote on last edited by
        #3

        Thanks for your reply... Christian Graus wrote: InnerText will give you all the inner nodes as well. Use .Value instead. So how should this line be formed then ?? 'Dim SizeData As String =   node.Attributes.GetNamedItem("SizeData").Value I have tried: Dim subnode1 = node.SelectSingleNode("SizeData").Value Dim Size As String = subnode1.InnerText But this won't compile... Any thoughts??

        C 1 Reply Last reply
        0
        • N nhsal69

          Thanks for your reply... Christian Graus wrote: InnerText will give you all the inner nodes as well. Use .Value instead. So how should this line be formed then ?? 'Dim SizeData As String =   node.Attributes.GetNamedItem("SizeData").Value I have tried: Dim subnode1 = node.SelectSingleNode("SizeData").Value Dim Size As String = subnode1.InnerText But this won't compile... Any thoughts??

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Perhaps if you told me the error, but .Value should return a string, not a node. That's the point, it REPLACES the call to InnerText, which you can't use, it won't work.

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          N 1 Reply Last reply
          0
          • C Christian Graus

            Perhaps if you told me the error, but .Value should return a string, not a node. That's the point, it REPLACES the call to InnerText, which you can't use, it won't work.

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            N Offline
            N Offline
            nhsal69
            wrote on last edited by
            #5

            Sorry, I'm pretty new to this and am now a bit confused. In the original code above: Dim fullpath As String = node.Attributes.GetNamedItem("fullpath").Value Works and returns a value for "FullPath" Dim SizeData As String = node.Attributes.GetNamedItem("SizeData").Value Doesn't work and gives an "System.NullReferenceException" Error when compiled. When that line is commented out the code returns all expected results. Am I missing something here as the "fullpath" line in the XML is of the same form as the "SizeData" one.. So why do I get the error???

            J 1 Reply Last reply
            0
            • N nhsal69

              Sorry, I'm pretty new to this and am now a bit confused. In the original code above: Dim fullpath As String = node.Attributes.GetNamedItem("fullpath").Value Works and returns a value for "FullPath" Dim SizeData As String = node.Attributes.GetNamedItem("SizeData").Value Doesn't work and gives an "System.NullReferenceException" Error when compiled. When that line is commented out the code returns all expected results. Am I missing something here as the "fullpath" line in the XML is of the same form as the "SizeData" one.. So why do I get the error???

              J Offline
              J Offline
              John M Bundy
              wrote on last edited by
              #6

              You are going about it in a kind of roundabout way (i prefer XPath) but I digress, you were on the correct path with your "subnode" line. Following your method, I just add two lines (your original subnode is the same).

              Dim subnode = node.SelectSingleNode("Name")
              Dim subnode2 = node.SelectSingleNode("SizeData")
              Dim SizeData As String = subnode2.Attributes.GetNamedItem("Size").Value

              N 1 Reply Last reply
              0
              • J John M Bundy

                You are going about it in a kind of roundabout way (i prefer XPath) but I digress, you were on the correct path with your "subnode" line. Following your method, I just add two lines (your original subnode is the same).

                Dim subnode = node.SelectSingleNode("Name")
                Dim subnode2 = node.SelectSingleNode("SizeData")
                Dim SizeData As String = subnode2.Attributes.GetNamedItem("Size").Value

                N Offline
                N Offline
                nhsal69
                wrote on last edited by
                #7

                Fantastic.. it's working.... here is the final code :

                    Try
                
                        Dim odoc As New System.Xml.XmlDocument
                        odoc.Load("C:\\test\\10g\_2.xml")
                        Dim oXmlLog As System.Xml.XmlElement
                        Dim text As String = ""
                        For Each oXmlLog In odoc.SelectNodes("Root")
                            Dim node As System.Xml.XmlElement
                            For Each node In oXmlLog.SelectNodes("Folder")
                                Dim fullpath As String = node.Attributes.GetNamedItem("fullpath").Value
                
                                'Dim SizeData As String = node.Attributes.GetNamedItem("SizeData").Value
                
                                Dim subnode = node.SelectSingleNode("Name")
                                Dim name As String = subnode.InnerText
                
                                Dim subnode2 = node.SelectSingleNode("SizeData")
                                Dim SizeData As String = subnode2.Attributes.GetNamedItem("Size").Value
                
                
                                Dim Date\_ As XmlElement = odoc.DocumentElement
                                Dim Date\_time As XmlNodeList = Date\_.ChildNodes
                                Dim Date\_\_time = (Date\_time(0).InnerText)
                
                
                
                                text &= Date\_\_time & " " & fullpath & " " & name & " " & SizeData & Environment.NewLine
                            Next
                        Next
                
                        Console.Write(text)
                        Console.Read()
                    Catch ex As Exception
                        Console.Write(ex.ToString())
                        Console.Read()
                    End Try
                

                Now I just need to get it to pass this info to SQL.. Thanks again...

                J 1 Reply Last reply
                0
                • N nhsal69

                  Fantastic.. it's working.... here is the final code :

                      Try
                  
                          Dim odoc As New System.Xml.XmlDocument
                          odoc.Load("C:\\test\\10g\_2.xml")
                          Dim oXmlLog As System.Xml.XmlElement
                          Dim text As String = ""
                          For Each oXmlLog In odoc.SelectNodes("Root")
                              Dim node As System.Xml.XmlElement
                              For Each node In oXmlLog.SelectNodes("Folder")
                                  Dim fullpath As String = node.Attributes.GetNamedItem("fullpath").Value
                  
                                  'Dim SizeData As String = node.Attributes.GetNamedItem("SizeData").Value
                  
                                  Dim subnode = node.SelectSingleNode("Name")
                                  Dim name As String = subnode.InnerText
                  
                                  Dim subnode2 = node.SelectSingleNode("SizeData")
                                  Dim SizeData As String = subnode2.Attributes.GetNamedItem("Size").Value
                  
                  
                                  Dim Date\_ As XmlElement = odoc.DocumentElement
                                  Dim Date\_time As XmlNodeList = Date\_.ChildNodes
                                  Dim Date\_\_time = (Date\_time(0).InnerText)
                  
                  
                  
                                  text &= Date\_\_time & " " & fullpath & " " & name & " " & SizeData & Environment.NewLine
                              Next
                          Next
                  
                          Console.Write(text)
                          Console.Read()
                      Catch ex As Exception
                          Console.Write(ex.ToString())
                          Console.Read()
                      End Try
                  

                  Now I just need to get it to pass this info to SQL.. Thanks again...

                  J Offline
                  J Offline
                  John M Bundy
                  wrote on last edited by
                  #8

                  Just because it might be easier for you to work with, take a look at this method. Drag a datagridview onto your form designer and put in the following code, change the table to see your different output. VS is very good at converting between XML and Datasets and vice-versa. If nothing else, the tables will help you visualize the structure of the XML, you can easily see from this sample that size is on a different 'level' than the rest of the data.

                  Imports System.IO
                  Public Class Form1
                  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                  'load xml into a dataset to use here
                  Dim dS As New DataSet
                  Dim fS As FileStream
                  'open the xml file so we can use it to fill the dataset
                  fS = New FileStream("C:\test\10g_2.xml", FileMode.Open)
                  'fill the dataset
                  Try
                  dS.ReadXml(fS)
                  Catch ex As Exception
                  MsgBox(ex)
                  Finally
                  fS.Close()
                  End Try
                  Me.DataGridView1.DataSource = dS.Tables(2)

                  End Sub
                  

                  End Class

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups