// Merges the tree of Elements whose root is <code>childElement</code> // underneath the <code>parentElement</code>. // // If the <code>parentElement</code> already has an element that matches // the <code>childElement</code>, then recursively attaches the // grandchildren instead. // // The element returned will be either the supplied // <code>childElement</code>, or an existing child element if one already // existed under <code>parentElement</code>. private static XElement MergeTree(XElement parentElement, XElement childElement) { var childElementOid = NofMetaModel.GetAttribute(childElement, "oid"); if (childElementOid != null) { // before we add the child element, check to see if it is already // there var existingChildElements = ElementsUnder(parentElement, childElement.Name.LocalName); foreach (var possibleMatchingElement in existingChildElements) { var possibleMatchOid = NofMetaModel.GetAttribute(possibleMatchingElement, "oid"); if (possibleMatchOid == null || !possibleMatchOid.Equals(childElementOid)) { continue; } // match: transfer the children of the child (grandchildren) to the // already existing matching child var existingChildElement = possibleMatchingElement; var grandchildrenElements = ElementsUnder(childElement, "*"); foreach (var grandchildElement in grandchildrenElements) { grandchildElement.Remove(); MergeTree(existingChildElement, grandchildElement); } return(existingChildElement); } } parentElement.Add(childElement); return(childElement); }
// Merges the tree of Elements whose root is <code>childElement</code> // underneath the <code>parentElement</code>. // // If the <code>parentElement</code> already has an element that matches // the <code>childElement</code>, then recursively attaches the // grandchildren instead. // // The element returned will be either the supplied // <code>childElement</code>, or an existing child element if one already // existed under <code>parentElement</code>. private static XElement MergeTree(XElement parentElement, XElement childElement) { Log.Debug("mergeTree(" + DoLog("parent", parentElement) + AndLog("child", childElement)); string childElementOid = NofMetaModel.GetAttribute(childElement, "oid"); Log.Debug("mergeTree(El,El): " + DoLog("childOid", childElementOid)); if (childElementOid != null) { // before we add the child element, check to see if it is already // there Log.Debug("mergeTree(El,El): check if child already there"); IEnumerable <XElement> existingChildElements = ElementsUnder(parentElement, childElement.Name.LocalName); foreach (XElement possibleMatchingElement in existingChildElements) { string possibleMatchOid = NofMetaModel.GetAttribute(possibleMatchingElement, "oid"); if (possibleMatchOid == null || !possibleMatchOid.Equals(childElementOid)) { continue; } Log.Debug("mergeTree(El,El): child already there; merging grandchildren"); // match: transfer the children of the child (grandchildren) to the // already existing matching child XElement existingChildElement = possibleMatchingElement; IEnumerable <XElement> grandchildrenElements = ElementsUnder(childElement, "*"); foreach (XElement grandchildElement in grandchildrenElements) { grandchildElement.Remove(); Log.Debug("mergeTree(El,El): merging " + DoLog("grandchild", grandchildElement)); MergeTree(existingChildElement, grandchildElement); } return(existingChildElement); } } parentElement.Add(childElement); return(childElement); }