PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Getting the sum of a specific attribute in a node list-VBForums
Results 1 to 7 of 7

Thread: Getting the sum of a specific attribute in a node list

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2016
    Posts
    11

    Getting the sum of a specific attribute in a node list

    I'm trying to get the sum of the @valueOf attribute from a node list. There can be x number of records

    if for instance there are three records , my code below only gets the first record 3 times


    Dim _values As XmlNodeList = _temp_xml_Doc.SelectNodes("/BACSDocument/Data/ARUDD/Advice/OriginatingAccountRecords/OriginatingAccountRecord/ReturnedDebitItem")
    Dim Counter As Integer = 0
    For Counter = 1 To _values.Count
    Dim x As XmlAttribute = _temp_xml_Doc.SelectSingleNode("/BACSDocument/Data/ARUDD/Advice/OriginatingAccountRecords/OriginatingAccountRecord/ReturnedDebitItem/@valueOf")
    _totalvalue = _totalvalue + CDec(x.Value)
    Next

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

    Re: Getting the sum of a specific attribute in a node list

    Wouldn't it make more sense to do something like this:
    vb.net Code:
    1. Dim nodes As XmlNodeList = _temp_xml_Doc.SelectNodes("/BACSDocument/Data/ARUDD/Advice/OriginatingAccountRecords/OriginatingAccountRecord/ReturnedDebitItem")
    2.  
    3. For Each node In nodes
    4.     'Use node here.
    5. Next
    You need to get feel for what type of loop to use in what situation. If you have a list and you want to do something for each item in that list, a For Each loop is the obvious choice. You would only use a For loop if you had a use for the loop counter. If the only use you might have is as an index into a list then you should be using a For Each loop unless the Step for the For loop is something other than 1.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2016
    Posts
    11

    Re: Getting the sum of a specific attribute in a node list

    Hi,

    I have already tried that code, but just to make sure I tried it again

    Dim _values As XmlNodeList = _temp_xml_Doc.SelectNodes("/BACSDocument/Data/ARUDD/Advice/OriginatingAccountRecords/OriginatingAccountRecord/ReturnedDebitItem")
    For Each _returnewdvalue In _values
    Dim x As XmlAttribute = _temp_xml_Doc.SelectSingleNode("/BACSDocument/Data/ARUDD/Advice/OriginatingAccountRecords/OriginatingAccountRecord/ReturnedDebitItem/@valueOf")
    WrtietoErrorLog(x.Value.ToString)
    _totalvalue = _totalvalue + CDec(x.Value)
    Next
    on each loop I wrote the x.Value to a text file. In the case of this test XML file there are three records , all with different values, however all I get is the @valurOf the first record , three times. Its appears to only be picking up the first record.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,209

    Re: Getting the sum of a specific attribute in a node list

    That is because you haven't really done anything to change the behaviour of the code... you are still setting x to _temp_xml_Doc.SelectSingleNode(...) in the same way.

    Instead of that, you should be using the loop variable _returnewdvalue which contains the current item from the _values list.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,038

    Re: Getting the sum of a specific attribute in a node list

    Where the comment in my code says "Use node here." I literally meant to use the 'node' variable, i.e. the loop control variable. As si says, that's the rather poorly named '_returnewdvalue' in your code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2016
    Posts
    11

    Re: Getting the sum of a specific attribute in a node list

    Thank you both, Jmcilhinney and si_the_geek, I think I understand now. I shall go and try a few things, see if I can work it out now you have pointed me in the right direction. If I get stuck, I'll come back

    thanks again

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,038

    Re: Getting the sum of a specific attribute in a node list

    I've never actually used an XmlNodeList myself so I don't know all the ins and outs. I just took a look at the documentation and it appears that it implements IEnumerable, so you can use a For Each loop, but it doesn't implement IEnumerable(Of T), so the default type for the loop control variable will be Object. You'll need to declare that variable as the actual type of the nodes in order to use them as that type. I don't know what that type would be but I'm assuming that it would be XmlNode. In that case, you might do something like this:
    vb.net Code:
    1. Dim nodes As XmlNodeList = _temp_xml_Doc.SelectNodes("/BACSDocument/Data/ARUDD/Advice/OriginatingAccountRecords/OriginatingAccountRecord/ReturnedDebitItem")
    2. Dim sum As Integer
    3.  
    4. For Each node As XmlNode In nodes
    5.     Dim attributeValue = CInt(node.Attributes("@valueOf"))
    6.  
    7.     sum += attributeValue
    8. Next
    If that works, you could do this instead:
    vb.net Code:
    1. Dim sum = nodes.Cast(Of XmlNode)().Sum(Function(node) CInt(node.Attributes("@valueOf")))
    There may even be a better way using LINQ to XML specifically but I don't deal with XML much so I'm not up-to-date with everything you can do with it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width