Skip to content

Commit 10ccea8

Browse files
committed
fix(XMLDiff): refactor add operation logic to handle cases with no original last child, i.e. no child at all and ensure proper addition of modified children
1 parent cf94be7 commit 10ccea8

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

XMLDiff/Program.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -584,20 +584,26 @@ private static bool CompareElements(XDocument original, XDocument modified, XEle
584584
if (j + 1 <= modifiedChildren.Count)
585585
{
586586
XElement? originalLast = originalChildren.LastOrDefault();
587+
XElement addOp;
587588
if (originalLast != null) {
588-
XElement addOp = new XElement("add",
589+
addOp = new XElement("add",
589590
new XAttribute("sel", GenerateXPath(originalLast, onlyFullPath)),
590591
new XAttribute("pos", "after")
591592
);
592-
while (j < modifiedChildren.Count)
593-
{
594-
var addedChild = modifiedChildren[j];
595-
addOp.Add(addedChild);
596-
Logger.Debug($"Added element '{addedChild.Name}' to parent '{originalElem.Name}'.");
597-
j++;
598-
}
599-
diffRoot.Add(addOp);
600593
}
594+
else {
595+
addOp = new XElement("add",
596+
new XAttribute("sel", GenerateXPath(originalElem, onlyFullPath))
597+
);
598+
}
599+
while (j < modifiedChildren.Count)
600+
{
601+
var addedChild = modifiedChildren[j];
602+
addOp.Add(addedChild);
603+
Logger.Debug($"Added element '{addedChild.Name}' to parent '{originalElem.Name}'.");
604+
j++;
605+
}
606+
diffRoot.Add(addOp);
601607
}
602608
return false;
603609
}

0 commit comments

Comments
 (0)