@@ -63,10 +63,14 @@ internal ComparisonState CompareNodes(XmlNode control,
6363 XPathContext controlContext ,
6464 XmlNode test ,
6565 XPathContext testContext ) {
66+ IEnumerable < XmlNode > allControlChildren =
67+ control . ChildNodes . Cast < XmlNode > ( ) ;
6668 IEnumerable < XmlNode > controlChildren =
67- control . ChildNodes . Cast < XmlNode > ( ) . Where ( n => NodeFilter ( n ) ) ;
69+ allControlChildren . Where ( n => NodeFilter ( n ) ) ;
70+ IEnumerable < XmlNode > allTestChildren =
71+ test . ChildNodes . Cast < XmlNode > ( ) ;
6872 IEnumerable < XmlNode > testChildren =
69- test . ChildNodes . Cast < XmlNode > ( ) . Where ( n => NodeFilter ( n ) ) ;
73+ allTestChildren . Where ( n => NodeFilter ( n ) ) ;
7074
7175 return Compare ( new Comparison ( ComparisonType . NODE_TYPE ,
7276 control , GetXPath ( controlContext ) ,
@@ -94,8 +98,10 @@ internal ComparisonState CompareNodes(XmlNode control,
9498 // and finally recurse into children
9599 . AndIfTrueThen ( control . NodeType != XmlNodeType . Attribute ,
96100 CompareChildren ( controlContext ,
101+ allControlChildren ,
97102 controlChildren ,
98103 testContext ,
104+ allTestChildren ,
99105 testChildren ) ) ;
100106 }
101107
@@ -163,19 +169,21 @@ private ComparisonState NodeTypeSpecificComparison(XmlNode control,
163169 }
164170
165171 private Func < ComparisonState > CompareChildren ( XPathContext controlContext ,
172+ IEnumerable < XmlNode > allControlChildren ,
166173 IEnumerable < XmlNode > controlChildren ,
167174 XPathContext testContext ,
175+ IEnumerable < XmlNode > allTestChildren ,
168176 IEnumerable < XmlNode > testChildren ) {
169177
170178 return ( ) => {
171179 controlContext
172- . SetChildren ( controlChildren . Select < XmlNode , XPathContext . INodeInfo >
180+ . SetChildren ( allControlChildren . Select < XmlNode , XPathContext . INodeInfo >
173181 ( ElementSelectors . TO_NODE_INFO ) ) ;
174182 testContext
175- . SetChildren ( testChildren . Select < XmlNode , XPathContext . INodeInfo >
183+ . SetChildren ( allTestChildren . Select < XmlNode , XPathContext . INodeInfo >
176184 ( ElementSelectors . TO_NODE_INFO ) ) ;
177- return CompareNodeLists ( controlChildren , controlContext ,
178- testChildren , testContext ) ;
185+ return CompareNodeLists ( allControlChildren , controlChildren , controlContext ,
186+ allTestChildren , testChildren , testContext ) ;
179187 } ;
180188 }
181189
@@ -449,15 +457,19 @@ private ComparisonState CompareProcessingInstructions(XmlProcessingInstruction c
449457 /// Also performs CHILD_LOOKUP comparisons for each node that
450458 /// couldn't be matched to one of the "other" list.
451459 /// </remarks>
452- private ComparisonState CompareNodeLists ( IEnumerable < XmlNode > controlSeq ,
460+ private ComparisonState CompareNodeLists ( IEnumerable < XmlNode > allControlChildren ,
461+ IEnumerable < XmlNode > controlSeq ,
453462 XPathContext controlContext ,
463+ IEnumerable < XmlNode > allTestChildren ,
454464 IEnumerable < XmlNode > testSeq ,
455465 XPathContext testContext ) {
456466
457467 ComparisonState chain = new OngoingComparisonState ( this ) ;
458468
459469 IEnumerable < KeyValuePair < XmlNode , XmlNode > > matches =
460470 NodeMatcher . Match ( controlSeq , testSeq ) ;
471+ IList < XmlNode > controlListForXpath = new List < XmlNode > ( allControlChildren ) ;
472+ IList < XmlNode > testListForXpath = new List < XmlNode > ( allTestChildren ) ;
461473 IList < XmlNode > controlList = new List < XmlNode > ( controlSeq ) ;
462474 IList < XmlNode > testList = new List < XmlNode > ( testSeq ) ;
463475 ICollection < XmlNode > seen = new HashSet < XmlNode > ( ) ;
@@ -466,10 +478,12 @@ private ComparisonState CompareNodeLists(IEnumerable<XmlNode> controlSeq,
466478 seen . Add ( control ) ;
467479 XmlNode test = pair . Value ;
468480 seen . Add ( test ) ;
481+ int controlIndexForXpath = controlListForXpath . IndexOf ( control ) ;
482+ int testIndexForXpath = testListForXpath . IndexOf ( test ) ;
469483 int controlIndex = controlList . IndexOf ( control ) ;
470484 int testIndex = testList . IndexOf ( test ) ;
471- controlContext . NavigateToChild ( controlIndex ) ;
472- testContext . NavigateToChild ( testIndex ) ;
485+ controlContext . NavigateToChild ( controlIndexForXpath ) ;
486+ testContext . NavigateToChild ( testIndexForXpath ) ;
473487 try {
474488 chain =
475489 chain . AndThen ( new Comparison ( ComparisonType . CHILD_NODELIST_SEQUENCE ,
0 commit comments