/// <inheritdoc cref="ElementSelector"/> public bool CanBeCompared(XmlElement controlElement, XmlElement testElement) { XmlElement currentControl = controlElement; XmlElement currentTest = testElement; // match on element names only for leading levels for (int currentLevel = 0; currentLevel <= levels - 2; currentLevel++) { if (!ElementSelectors.ByName(currentControl, currentTest) || !currentControl.HasChildNodes || !currentTest.HasChildNodes) { return(false); } XmlNode n1 = GetFirstEligibleChild(currentControl); XmlNode n2 = GetFirstEligibleChild(currentTest); if (n1 is XmlElement && n2 is XmlElement) { currentControl = n1 as XmlElement; currentTest = n2 as XmlElement; } else { return(false); } } // finally compare the level containing the text child node return(ElementSelectors.ByNameAndText(currentControl, currentTest)); }
/// <inheritdoc cref="ElementSelector"/> public static bool CanBeCompared(XmlElement controlElement, XmlElement testElement) { if (!ElementSelectors.ByNameAndText(controlElement, testElement)) { return(false); } XmlNodeList controlChildren = controlElement.ChildNodes; XmlNodeList testChildren = testElement.ChildNodes; int controlLen = controlChildren.Count; int testLen = testChildren.Count; int controlIndex, testIndex; for (controlIndex = testIndex = 0; controlIndex < controlLen && testIndex < testLen; ) { // find next non-text child nodes XmlNode c = FindNonText(controlChildren, ref controlIndex, controlLen); if (IsText(c)) { break; } XmlNode t = FindNonText(testChildren, ref testIndex, testLen); if (IsText(t)) { break; } // different types of children make elements // non-comparable if (c.NodeType != t.NodeType) { return(false); } // recurse for child elements if (c is XmlElement && !CanBeCompared(c as XmlElement, t as XmlElement)) { return(false); } controlIndex++; testIndex++; } // child lists exhausted? if (controlIndex < controlLen) { FindNonText(controlChildren, ref controlIndex, controlLen); // some non-Text children remained if (controlIndex < controlLen) { return(false); } } if (testIndex < testLen) { FindNonText(testChildren, ref testIndex, testLen); // some non-Text children remained if (testIndex < testLen) { return(false); } } return(true); }