Results 1 to 5 of 5

Thread: Read XML File

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2016
    Posts
    24

    Read XML File

    Hello,

    i am looking for help i want to extract the innertext from a childnode in an xml file and put it in a listbox.

    here is a part of the xml, i want to extract "2019401001503.geo" from the xml and put this in an listbox the xml files contains several of these "parts"

    could someone help me?

    in attachement the XML file

    thank you in advance,

    Dumon.xml

    Code:
     <Part PartNo="2019401001503">
          <Barcode>0300000698</Barcode>
          <BasicMaterial>1.0038 SPZ</BasicMaterial>
          <RawMaterial>SPZM0150---</RawMaterial>
          <PartNoExt />
          <Description>2019401001503</Description>
          <DrawingNo />
          <AreaUnit>m2</AreaUnit>
          <CadFilePath>Artikel\600\698</CadFilePath>
          <CadFileName>2019401001503.geo</CadFileName>
          <ImageFilePath>Artikel\600\698</ImageFilePath>
          <ImageFileName>FabGenerated_698.jpg</ImageFileName>
          <WeightUnit>kg</WeightUnit>
          <TotalQuantityInJob>1</TotalQuantityInJob>
          <CuttingLengthPartUnit>mm</CuttingLengthPartUnit>
          <Dimensions>
            <Unit>mm</Unit>
            <Length>2630.000</Length>
            <Width>500.000</Width>
            <Thickness>1.500</Thickness>
          </Dimensions>
          <Area>1.270</Area>
          <Weight>0.000</Weight>
          <TargetProcessingTimePerPiece>1.460</TargetProcessingTimePerPiece>
          <CuttingLengthPart>7195.300</CuttingLengthPart>
    Last edited by dday9; Sep 14th, 2021 at 08:09 AM. Reason: Added code tags

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: Read XML File

    What have you tried and where are you stuck? There's information about reading XML files all over the place so you'll be able to find that information if you look for it. If you don't try to do it then you don't know that you can't do it, so you don't know whether you even need help.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,391

    Re: Read XML File

    BASED on what you posted. Note I used a literal for testing.

    Code:
            Dim xe As XElement
            ' xe = XElement.Load("path here")
    
            xe = <Part PartNo="2019401001503">
                     <Barcode>0300000698</Barcode>
                     <BasicMaterial>1.0038 SPZ</BasicMaterial>
                     <RawMaterial>SPZM0150---</RawMaterial>
                     <PartNoExt/>
                     <Description>2019401001503</Description>
                     <DrawingNo/>
                     <AreaUnit>m2</AreaUnit>
                     <CadFilePath>Artikel\600\698</CadFilePath>
                     <CadFileName>2019401001503.geo</CadFileName>
                     <ImageFilePath>Artikel\600\698</ImageFilePath>
                     <ImageFileName>FabGenerated_698.jpg</ImageFileName>
                     <WeightUnit>kg</WeightUnit>
                     <TotalQuantityInJob>1</TotalQuantityInJob>
                     <CuttingLengthPartUnit>mm</CuttingLengthPartUnit>
                     <Dimensions>
                         <Unit>mm</Unit>
                         <Length>2630.000</Length>
                         <Width>500.000</Width>
                         <Thickness>1.500</Thickness>
                     </Dimensions>
                     <Area>1.270</Area>
                     <Weight>0.000</Weight>
                     <TargetProcessingTimePerPiece>1.460</TargetProcessingTimePerPiece>
                     <CuttingLengthPart>7195.300</CuttingLengthPart>
                 </Part>
    
            Dim CadFN As XElement = (From el In xe.<CadFileName> Select el Take 1).FirstOrDefault
            ListBox1.Items.Add(CadFN.Value)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,461

    Re: Read XML File

    You can use the XDocument's Load method (documentation) to read the file. You can then use the XContainer's Descendants method (documentation) to get every CadFileName element. The final step would be to loop over the results and get the Value property (documentation).

    Here is an example:
    Code:
    Dim document = XDocument.Load("my-file-here.xml")
    Dim cadFileNames = document.Descendants("CadFileName")
    For Each cadFileName In cadFileNames
        Dim value = cadFileName.Value ' do something with the value
    Next
    Fiddle: https://dotnetfiddle.net/uoiEcW
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2016
    Posts
    24

    Re: Read XML File

    hello,

    i was able to figure it out. i am getting the ellements by the underneath code

    Code:
    Try
                ListBox1.Items.Clear()
                list.Clear()
                Dim xmldoc As New XmlDataDocument()
                Dim xmlnode As XmlNodeList
                Dim i As Integer
                Dim str As String
                Dim fs As New FileStream(file1, FileMode.Open, FileAccess.Read)
                xmldoc.Load(fs)
                xmlnode = xmldoc.GetElementsByTagName("CadFileName")
    
                For i = 0 To xmlnode.Count - 1
                    xmlnode(i).ChildNodes.Item(0).InnerText.Trim()
                    ''str = xmlnode(i).ChildNodes.Item(0).InnerText.Trim() & "  " & xmlnode(i).ChildNodes.Item(1).InnerText.Trim() & "  " & xmlnode(i).ChildNodes.Item(2).InnerText.Trim()
                    str = xmlnode(i).InnerText
                    list.Add(str)
                    ''MsgBox(str)
                    ListBox1.Items.Add(str)
                Next
            Catch ex As Exception
                MessageBox.Show("something went wrong")
            End Try

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