locked
Movetochild or selectchildren method not working in foreach loop RRS feed

  • Question

  • I have the following code that loops through the XPathNodeIterator "CPDDosimeterDataNodes".  Although there are child nodes with the local name "CPDType", the MoveToChild method returns a false and the SelectChildren.Count method returns a 0.
          foreach (XPathNavigator CPDnodes in CPDDosimeterDataNodes)
          {
            //Boolean d = CPDnodes.MoveToChild("CPDType", CPDnodes.NamespaceURI);
            int CPDnodecount = CPDnodes.SelectChildren("CPDType", CPDnodes.NamespaceURI).Count;
            namecount = namecount + CPDnodecount;
          }

    Here is the innerXML of CPDnodes

    <CPDCrewID xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-02-22T15:33:23">368</CPDCrewID><CPDName xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-02-22T15:33:23"></CPDName><DosimeterID xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-02-22T15:33:23"/><CPDHighLETDose xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-02-22T15:33:23"/><CPDHighLETDoseError xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-02-22T15:33:23"/><CPDDosimeterDetail xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-02-22T15:33:23"><CPDType>Luxel-300</CPDType><CPDMeasuredDose/><CPDMeasuredDoseError/><CPDLowLETDose/><CPDLowLETDoseError/><CPDTotalDose/><CPDTotalDoseError/><CPDDoseEquivalent/><CPDDoseEquivalentError/><CPDQF/><CPDQFError/></CPDDosimeterDetail>
    
    
    
    
    
    
    
    


     Please give me a clue what I am doing wrong!

    Thanks


    Charles Picco


    Monday, July 11, 2011 3:15 PM

Answers

  • Yes, I get that these are repeating (why else would you need to count), but you don't need to loop through just to count.  Do you really need this in code anyway?  I always go with what's built into InfoPath before I resort to coding.

    You can add a field called cpdTypeCount, edit the default value, insert the count() function, and insert the CPDType field into the parenthesis

    Then, if you do need the value in code all you do is pull in the cpdTypeCount field's value.

    • Marked as answer by Charles Picco Monday, July 11, 2011 10:07 PM
    Monday, July 11, 2011 8:44 PM

All replies

  • You don't really need to loop through them to get the count (actually you could even do the count without code if you don't need the count in your code):

    // Create XPathNavigator and specify XPath for nodes.
    XPathNavigator repeatingTableRow1 = MainDataSource.CreateNavigator().SelectSingleNode( "/my:myFields/my:group1/my:group2[1]", NamespaceManager);

    // Select nodes in specified XPathNavigator.
    CurrentView.SelectNodes(repeatingTableRow1);

    // Get selected nodes.
    XPathNodeIterator selectedNodes = CurrentView.GetSelectedNodes();

    // Display the count of selected nodes.
    MessageBox.Show(selectedNodes.Count.ToString()); 

    (I borrowed the sample code from here:  http://social.msdn.microsoft.com/Forums/en/sharepointinfopath/thread/d30d6619-6087-449d-afea-b5b2d2bd7d7b)

    Monday, July 11, 2011 6:25 PM
  • I should have been more clear. There are more than one CPDNodes for each CPDDosimeterDataNodes and there are move then one "CPDtype" per CPDnodes.
    Charles Picco
    Monday, July 11, 2011 8:00 PM
  • Yes, I get that these are repeating (why else would you need to count), but you don't need to loop through just to count.  Do you really need this in code anyway?  I always go with what's built into InfoPath before I resort to coding.

    You can add a field called cpdTypeCount, edit the default value, insert the count() function, and insert the CPDType field into the parenthesis

    Then, if you do need the value in code all you do is pull in the cpdTypeCount field's value.

    • Marked as answer by Charles Picco Monday, July 11, 2011 10:07 PM
    Monday, July 11, 2011 8:44 PM