retrieval of XML information & Copy to excel Files Pin

dev_psychos

New member
Joined
Jun 20, 2019
Messages
2
Programming Experience
Beginner
Hello everyone ,

I am trying to retrieve XML information, I managed to recover the attributes against the childrens they are not recovering below my code and the form of my XML file:

XML template :
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
   <ToolsList>
      <Tool PartNumber="0018B">
         <ToolLine Text=" FI : 3897" />
         <ToolLine Text=" PINCE1 Reference : M 22520/2.01" />
         <ToolLine Text=" Position : 2.02" />
         <ToolLine Text=" Reglage : 7" />
         <ToolLine Text=" PINCE2 Reference: M 22520/7.01" />
      </Tool>
   </ToolsList>
</Root>

Code VB :
VB.NET:
Imports System.IO
Imports System.Xml
Module ParsingUsingXmlDocument
    Sub Main()
        Try
            Dim m_xmld As XmlDocument
            Dim m_nodelist As XmlNodeList
            Dim m_node As XmlNode
            'Create the XML Document
            m_xmld = New XmlDocument()
            'Load the Xml file
            m_xmld.Load("D:\Users\Desktop\Xml to excel chahine\U881A1013_105_A_PREP_1.xml")
            m_nodelist = m_xmld.SelectNodes("/Root/ToolsList/PartNumber")
            'Loop through the nodes
            For Each m_node In m_nodelist
                Dim partnumber = m_node.Attributes.GetNamedItem("PartNumber").Value
                'Get the firstName Element Value
                Dim text1 = m_node.ChildNodes.Item(0).InnerText
                Dim text2 = m_node.ChildNodes.Item(1).InnerText
                Dim text3 = m_node.ChildNodes.Item(2).InnerText
                Dim text4 = m_node.ChildNodes.Item(3).InnerText
                Console.Write("partnumber: " & partnumber _
& " text1: " & text1 & " text3: " _
& text3)
                Console.Write(vbCrLf)
            Next
        Catch errorVariable As Exception
            'Error trapping
            Console.Write(errorVariable.ToString())
        End Try
    End Sub
End Module
thank you for the help
 
Last edited by a moderator:

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
Please edit your posts if possible. When posting XML or CODE click the INSERT drop down menu and insert the code to get proper formatting.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
I fixed the code formatting, just copy in Visual Studio and paste in the code box editor and indenting will be preserved.

.SelectNodes("/Root/ToolsList/PartNumber")
There is no PartNumber node, that is an attribute of the Tool node.
Further, ToolLine nodes are children of the Tool node.

I recommend you use the newer xml tools with XDocument and Linq instead of XmlDocument:
VB.NET:
Dim doc = XDocument.Load("data.xml")
For Each tool In doc...<Tool>
    Dim part = "PartNumber: " & tool.@PartNumber
    Dim lines = tool.<ToolLine>.Select(Function(item, ix) $" text{ix + 1}: {item.@Text}")
    Console.WriteLine(part & String.Concat(lines))
Next
Reading material can be found here: XML in Visual Basic | Microsoft Docs
 
Top Bottom