Results 1 to 9 of 9

Thread: [RESOLVED] XML and For Each Iteration

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    120

    Resolved [RESOLVED] XML and For Each Iteration

    I have XML like in this example, and what I want to achieve is to go through each item and catch some text when the criteria are met.

    XML SAMPLE:

    Code:
    <Item>
    <Attached_documents>
    <Attached_document_code>DUIM</Attached_document_code>
    <Text_to_catch>Some text to catch</Text_to_catch>
    <Attached_document_reference>More text</Attached_document_reference>
    <Attached_document_from_rule>1</Attached_document_from_rule>
    <Attached_document_date>02/22/24</Attached_document_date>
    </Attached_documents>
    <Trigger>PE2 00555061-055511/24</Trigger>
    <Packages>
    <Number_of_packages>31.00</Number_of_packages>
    <Marks1_of_packages>SHIPPMENT</Marks1_of_packages>
    <Marks2_of_packages></Marks2_of_packages>
    <Kind_of_packages_code>8A</Kind_of_packages_code>
    <Kind_of_packages_name>Wooden pack</Kind_of_packages_name>
    </Packages>
    </Item>
    <Item>
    <Attached_documents>
    <Attached_document_code>DQIM</Attached_document_code>
    <Text_to_catch>Some text to skip</Text_to_catch>
    <Attached_document_reference>More text</Attached_document_reference>
    <Attached_document_from_rule>1</Attached_document_from_rule>
    <Attached_document_date>02/22/24</Attached_document_date>
    </Attached_documents>
    <Trigger> </Trigger>
    <Packages>
    <Number_of_packages>31.00</Number_of_packages>
    <Marks1_of_packages>SHIPPMENT</Marks1_of_packages>
    <Marks2_of_packages></Marks2_of_packages>
    <Kind_of_packages_code>7F</Kind_of_packages_code>
    <Kind_of_packages_name>Wooden pack</Kind_of_packages_name>
    </Packages>
    </Item>
    <Item>
    <Attached_documents>
    <Attached_document_code>QWIM</Attached_document_code>
    <Text_to_catch>Some text to catch</Text_to_catch>
    <Attached_document_reference>More text</Attached_document_reference>
    <Attached_document_from_rule>1</Attached_document_from_rule>
    <Attached_document_date>02/22/24</Attached_document_date>
    </Attached_documents>
    <Trigger>PE2 66655061-055666/24</Trigger>
    <Packages>
    <Number_of_packages>21.00</Number_of_packages>
    <Marks1_of_packages>SHIPPMENT</Marks1_of_packages>
    <Marks2_of_packages></Marks2_of_packages>
    <Kind_of_packages_code>9B</Kind_of_packages_code>
    <Kind_of_packages_name>Glass bottle</Kind_of_packages_name>
    </Packages>
    </Item>
    Code should iterate and catch Text_to_catch IF Trigger has met criteria first two chars= "PE"

    How I imagine this code:

    Code:
    Dim MyCheck as String = Document.descendants("Trigger").Value
    Dim Short = MyCheck.Substring(0,2)
    
    For Each level1 in root.Elements("Item") ' where Short ="PE"
    
    MyText = Document.Descendants("Text_to_catch").Value
    
    Next
    Please be kind and write the code sample.

    The output should give me the following:
    Some text to catch
    Some text to catch

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,804

    Re: XML and For Each Iteration

    I have written this in steps instead of one big LINQ statement. FWIW - some of the naming is bad.

    First loading the test data you provided which is not valid. I also added identifiers to Text_to_catch

    Code:
            Dim xe As XElement
            'xe = XElement.Load("URI here")
    
            xe = <root>
                     <Item>
                         <Attached_documents>
                             <Attached_document_code>DUIM</Attached_document_code>
                             <Text_to_catch>A Some text to catch</Text_to_catch>
                             <Attached_document_reference>More text</Attached_document_reference>
                             <Attached_document_from_rule>1</Attached_document_from_rule>
                             <Attached_document_date>02/22/24</Attached_document_date>
                         </Attached_documents>
                         <Trigger>PE2 00555061-055511/24</Trigger>
                         <Packages>
                             <Number_of_packages>31.00</Number_of_packages>
                             <Marks1_of_packages>SHIPPMENT</Marks1_of_packages>
                             <Marks2_of_packages></Marks2_of_packages>
                             <Kind_of_packages_code>8A</Kind_of_packages_code>
                             <Kind_of_packages_name>Wooden pack</Kind_of_packages_name>
                         </Packages>
                     </Item>
                     <Item>
                         <Attached_documents>
                             <Attached_document_code>DQIM</Attached_document_code>
                             <Text_to_catch>B Some text to skip</Text_to_catch>
                             <Attached_document_reference>More text</Attached_document_reference>
                             <Attached_document_from_rule>1</Attached_document_from_rule>
                             <Attached_document_date>02/22/24</Attached_document_date>
                         </Attached_documents>
                         <Trigger></Trigger>
                         <Packages>
                             <Number_of_packages>31.00</Number_of_packages>
                             <Marks1_of_packages>SHIPPMENT</Marks1_of_packages>
                             <Marks2_of_packages></Marks2_of_packages>
                             <Kind_of_packages_code>7F</Kind_of_packages_code>
                             <Kind_of_packages_name>Wooden pack</Kind_of_packages_name>
                         </Packages>
                     </Item>
                     <Item>
                         <Attached_documents>
                             <Attached_document_code>QWIM</Attached_document_code>
                             <Text_to_catch>C Some text to catch</Text_to_catch>
                             <Attached_document_reference>More text</Attached_document_reference>
                             <Attached_document_from_rule>1</Attached_document_from_rule>
                             <Attached_document_date>02/22/24</Attached_document_date>
                         </Attached_documents>
                         <Trigger>PE2 66655061-055666/24</Trigger>
                         <Packages>
                             <Number_of_packages>21.00</Number_of_packages>
                             <Marks1_of_packages>SHIPPMENT</Marks1_of_packages>
                             <Marks2_of_packages></Marks2_of_packages>
                             <Kind_of_packages_code>9B</Kind_of_packages_code>
                             <Kind_of_packages_name>Glass bottle</Kind_of_packages_name>
                         </Packages>
                     </Item>
                 </root>
    And know the selection in two parts,

    Code:
            'get triggers of interest
            Dim triggers As IEnumerable(Of XElement)
            triggers = From el In xe...<Trigger>
                        Where el.Value.StartsWith("PE2")
                        Select el
    
            'and then the text
            Dim TextToCatch As IEnumerable(Of String)
    
            TextToCatch = From el In triggers
                             Select el.Parent.<Attached_documents>.<Text_to_catch>.Value
    
            'examine TextToCatch
    This all assumes that the sample is complete.
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    120

    Re: XML and For Each Iteration

    Code:
     Dim root As XElement
                root = Document.Root()
    
                Dim pcs As String
                Dim description As String
                Dim comdescription As String
                Dim weight As String
                Dim attdocuments As String
    
                For Each level1 In root.Elements("Item")
    
                    pcs = Document.Descendants("Suppplementary_unit_quantity").Value
                    description = Document.Descendants("Description_of_goods").Value
                    comdescription = Document.Descendants("Commercial_Description").Value
                    weight = Document.Descendants("Gross_weight_itm").Value
                    attdocuments = Document.Descendants("Attached_doc_item").Value
    
                    DataGridView1.Rows.Add(pcs, description, comdescription, weight)
                Next
    I can't make it work. For Each - catch the first item * how many items there are. I want to catch each item once.
    Name:  xml.jpg
Views: 38
Size:  28.8 KB

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    120

    Re: XML and For Each Iteration

    @dbasnett Thanks for your help. I couldn't make that sample work in my code

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,804

    Re: XML and For Each Iteration

    Quote Originally Posted by ivansmo View Post
    @dbasnett Thanks for your help. I couldn't make that sample work in my code
    Based on the XML you provided the code I posted does work. Maybe the XML sample was incorrect.
    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

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,804

    Re: XML and For Each Iteration

    Give this a try.

    Code:
            'put path in here
            xe = XElement.Load("path to XML")
    
            Dim pcs As String
            Dim description As String
            Dim comdescription As String
            Dim weight As String
            Dim attdocuments As String
            For Each itm As XElement In xe.<Item>
                pcs = itm...<Suppplementary_unit_quantity>.FirstOrDefault.Value
                description = itm...<Description_of_goods>.FirstOrDefault.Value
                comdescription = itm...<Commercial_Description>.FirstOrDefault.Value
                weight = itm...<Gross_weight_itm>.FirstOrDefault.Value
                attdocuments = itm...<Attached_doc_item>.FirstOrDefault.Value
                Stop
            Next
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    120

    Re: XML and For Each Iteration

    Great. @dbasnett your code works perfectly. It catches all the items and corresponding attributes.

    Code:
      Dim xe = XElement.Load(ofd.FileName)
    
                Dim pcs As String
                Dim description As String
                Dim comdescription As String
                Dim weight As String
                Dim attdocuments As String
                For Each itm As XElement In xe.<Item>
                    pcs = itm...<Suppplementary_unit_quantity>.FirstOrDefault.Value
                    description = itm...<Description_of_goods>.FirstOrDefault.Value
                    comdescription = itm...<Commercial_Description>.FirstOrDefault.Value
                    weight = itm...<Gross_weight_itm>.FirstOrDefault.Value
                    attdocuments = itm...<Attached_doc_item>.FirstOrDefault.Value
                    'Stop
                    DataGridView1.Rows.Add(pcs, description, comdescription, weight)
    
                Next
    Now, I need "filter" to catch only Item attributes where
    Code:
    <Trigger>PE2 00555061-055511/24</Trigger>
    starts with PE.

    I'll try this myself tomorrow, but if someone has an idea please write it here.

    Thanks

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,804

    Re: XML and For Each Iteration

    Guessing the location of the trigger element...

    Code:
            For Each itm As XElement In xe.<Item>.Where(Function(el) el.<Trigger>.FirstOrDefault IsNot Nothing AndAlso
                                                                        el.<Trigger>.Value.StartsWith("PE"))
                pcs = itm...<Suppplementary_unit_quantity>.FirstOrDefault.Value
                description = itm...<Description_of_goods>.FirstOrDefault.Value
                comdescription = itm...<Commercial_Description>.FirstOrDefault.Value
                weight = itm...<Gross_weight_itm>.FirstOrDefault.Value
                attdocuments = itm...<Attached_doc_item>.FirstOrDefault.Value
                Stop
            Next
    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    120

    Re: XML and For Each Iteration

    @dbasnett thanks. This code works perfectly.

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