protected void ResolveReferencesToReferences(XDocument document) { foreach (var element in document.Descendants()) { var target = element; var referenceAttribute = element.Attribute("reference"); var referencesCount = 0; while (referenceAttribute != null) { target = XPathHelper.GetElement(target, referenceAttribute.Value); referenceAttribute = target == null ? null : target.Attribute("reference"); referencesCount++; } if (target != null && referencesCount > 1) { element.SetReferenceTarget(target); } } }