Andrea Calabrò
Andrea Calabrò

Reputation: 59

Extract exact nodes from XML

I'm trying to extract data from exact nodes from many XML. Here an example of these XML:

<invoice>
  <details>
    <dssoc>Intel</dssoc>
    <doc>2124827</doc>
    <dtreg>2019-04-18</dtreg>
    <name>Hydro</name>
    <pariva>1586848579</pariva>
    <cdfis>3333666688</cdfis>
  </details>
  <details>
    <dssoc>Intel</dssoc>
    <doc>114785</doc>
    <dsfor>Hydro</dsfor>
    <pariva>857999315</pariva>
    <cdfis>255778933</cdfis>
    <revch>
      <dsrevc>Tax</dsrevc>
      <amount>32255.64</amount>
      <taxamount>7096.24</taxamount>
    </revch>
  </details>
  <filename>INVOICE0002413601.XML</filename>

I'm trying to extract "pariva" and "cdfis" nodes of the first "parent node" (as you can see pariva and cdfis nodes are present 2 times".

I've tried something like that:

Dim xmldoc As New XmlDataDocument()
Dim xmlnode As XmlNodeList
Dim fs As New FileStream(EiFile, FileMode.Open, FileAccess.Read)
xmldoc.Load(fs)
xmlnode = xmldoc.GetElementsByTagName("details")
pariva =xmlnode(0).ChildNodes.Item(4).InnerText.Trim()
cdfis =xmlnode(0).ChildNodes.Item(5).InnerText.Trim()

But this is not consistent because number of nodes for every XML can be different (so ChildNodes.Item(4) and (5) don't work with all XML).
Any tips for me? I'm using UiPath where I can invoke simple raw code in VBNet.

Thanks in advance!

Upvotes: 0

Views: 85

Answers (2)

G3nt_M3caj
G3nt_M3caj

Reputation: 2685

Try this one:

    Private Sub ElaboraNodi()

        Dim xmlS As String =
"<invoice>
  <details>
    <dssoc>Intel</dssoc>
    <doc>2124827</doc>
    <dtreg>2019-04-18</dtreg>
    <name>Hydro</name>
    <pariva>1586848579</pariva>
    <cdfis>3333666688</cdfis>
  </details>
  <details>
    <dssoc>Intel</dssoc>
    <doc>114785</doc>
    <dsfor>Hydro</dsfor>
    <pariva>857999315</pariva>
    <cdfis>255778933</cdfis>
    <revch>
      <dsrevc>Tax</dsrevc>
      <amount>32255.64</amount>
      <taxamount>7096.24</taxamount>
    </revch>
  </details>
  <filename>INVOICE0002413601.XML</filename>
</invoice>"


        Dim GetNode As Func(Of Xml.XmlNode, String, String) = Function(nodeList As Xml.XmlNode, tagName As String)
                                                                  Dim theChild As Xml.XmlNode = CType((From element In nodeList.ChildNodes
                                                                                                       Where CType(element, Xml.XmlNode).Name = tagName).FirstOrDefault, Xml.XmlNode)

                                                                  If theChild Is Nothing Then Return "Not found"
                                                                  Return theChild.InnerText
                                                              End Function



        Try


            Dim xmldoc As New Xml.XmlDocument
            xmldoc.LoadXml(xmlS)
            'Or load from path
            'xmldoc.Load(xmlPathFromFile) 

            Dim details As Xml.XmlNodeList = xmldoc.GetElementsByTagName("details")

            For Each item As Xml.XmlNode In details

                Dim codiceFiscale As String = GetNode(item, "cdfis")
                Dim partitaIva As String = GetNode(item, "pariva")
                Dim test As String = GetNode(item, "test")

                Console.WriteLine("codiceFiscale : " & codiceFiscale)
                Console.WriteLine("partitaIva : " & partitaIva)
                Console.WriteLine("test: " & test)

            Next

        Catch ex As Exception
            Console.WriteLine("Opsssss ops ha preso errore, probabilmente xml non conforme: " & ex.ToString)
        End Try

    End Sub

Upvotes: 1

dbasnett
dbasnett

Reputation: 11773

I find using XElement and LINQ easier

    Dim pth As String = EiFile 'or "your path here"
    Dim xe As XElement
    xe = XElement.Load(pth) 'load from file

    'FOR TESTING use literal, delete for production
    xe = <invoice>
             <details>
                 <dssoc>Intel</dssoc>
                 <doc>2124827</doc>
                 <dtreg>2019-04-18</dtreg>
                 <name>Hydro</name>
                 <pariva>1586848579</pariva>
                 <cdfis>3333666688</cdfis>
             </details>
             <details>
                 <dssoc>Intel</dssoc>
                 <doc>114785</doc>
                 <dsfor>Hydro</dsfor>
                 <pariva>857999315</pariva>
                 <cdfis>255778933</cdfis>
                 <revch>
                     <dsrevc>Tax</dsrevc>
                     <amount>32255.64</amount>
                     <taxamount>7096.24</taxamount>
                 </revch>
             </details>
             <filename>INVOICE0002413601.XML</filename>
         </invoice>

    Dim pariva As XElement
    Dim cdfis As XElement

    pariva = xe.<details>.<pariva>.FirstOrDefault
    cdfis = xe.<details>.<cdfis>.FirstOrDefault

    'the text is 
    '  pariva.Value
    '  cdfis.Value

Upvotes: 1

Related Questions