Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions XMLPatch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,21 @@ private void ApplyReplace(XElement replaceElement, XElement originalRoot)
{
if (targetObj is XElement target)
{
string targetName = target.Name.LocalName;
XElement? replaceSubElement = replaceElement.Element(targetName);
var replaceChildren = replaceElement.Elements().ToList();
XElement? parent = target.Parent;
string targetInfo = GetElementInfo(target);
string parentInfo = GetElementInfo(parent);
if (replaceSubElement != null)
if (replaceChildren.Count > 0)
{
string replaceInfo = GetElementInfo(replaceSubElement);
target.ReplaceWith(replaceSubElement);
Logger.Info($"Replaced element '{targetInfo}' with '{replaceInfo}' in '{parentInfo}'.");
// Insert all child elements from the replace operation
XNode lastInserted = target;
for (int i = replaceChildren.Count - 1; i >= 0; i--)
{
target.AddAfterSelf(replaceChildren[i]);
}
target.Remove();
string replaceInfo = GetElementInfo(replaceChildren[0]);
Logger.Info($"Replaced element '{targetInfo}' with '{replaceInfo}'{(replaceChildren.Count > 1 ? $" and {replaceChildren.Count - 1} sibling(s)" : "")} in '{parentInfo}'.");
}
else
{
Expand Down