
병합된 XML 파일에서 중복된 요소를 제거하려면, 자식 요소의 순서를 무시하는 옵션인 'ignoreChildOrder'를 사용한 후, 중복된 요소를 제거하는 로직을 추가하는 방법을 사용할 수 있습니다.
1. XMLDiffFile::merge 함수를 사용하여 두 개의 XML 파일을 병합합니다.
2. 병합된 XML 파일에서 중복된 요소를 찾기 위해 XPath를 사용합니다. 예를 들어, 중복된 요소의 이름을 "item"으로 가정하면, 다음과 같은 XPath를 사용할 수 있습니다. "//item[2]"
#hostingforum.kr
xml
item1
item2
item3
item2
item3
item4
결과:
item1
item2
childOrder를 무시한 병합 결과에 따라 3개 요소가 남아있을 수 있습니다.
item3
item4
위 예제에서, 자식 요소의 순서를 무시하는 옵션을 사용했기 때문에, 중복된 요소는 제거되지 않습니다. 따라서, 병합된 XML 파일에서 중복된 요소를 제거하려면, 별도의 로직을 추가해야 합니다.
다음은 예제 코드입니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
public class XmlDiffFile
{
public static void Main()
{
// 두 개의 XML 파일을 병합합니다.
XmlDocument doc1 = new XmlDocument();
doc1.Load("file1.xml");
XmlDocument doc2 = new XmlDocument();
doc2.Load("file2.xml");
XmlDocument mergedDoc = new XmlDocument();
mergedDoc.AppendChild(XMLDiffFile.merge(doc1.DocumentElement, doc2.DocumentElement, new MergeOptions { IgnoreChildOrder = true });
// 병합된 XML 파일에서 중복된 요소를 제거합니다.
var nodes = new List();
foreach (XmlNode node in mergedDoc.DocumentElement.ChildNodes)
{
if (nodes.Any(n => n.Name == node.Name && n.InnerText == node.InnerText))
continue;
nodes.Add(node);
}
// 중복된 요소를 제거한 후, XML 파일을 저장합니다.
XmlElement newRoot = newRootElement("root");
foreach (var node in nodes)
newRoot.AppendChild(newNode(elementName, node.Name, node.InnerText));
File.WriteAllText("output.xml", newXmlDocument(newRoot).OuterXml);
}
public static XmlElement merge(XmlElement elem1, XmlElement elem2, MergeOptions options)
{
// 자식 요소의 순서를 무시하는 옵션을 사용합니다.
if (options.IgnoreChildOrder)
{
// 자식 요소의 순서를 무시하는 옵션을 사용할 때, 중복된 요소를 제거하는 로직을 추가합니다.
return mergeWithOrderIgnored(elem1, elem2, options);
}
return mergeWithOrderPreserved(elem1, elem2, options);
}
private static XmlElement mergeWithOrderIgnored(XmlElement elem1, XmlElement elem2, MergeOptions options)
{
var nodes = new List();
foreach (XmlNode node in new List(elem1.ChildNodes.Concat(2).OrderBy(n => n.Name)) and new List(2).OrderBy(n => n.Name))
{
if (nodes.Any(n => n.Name == node.Name && n.InnerText == node.InnerText))
continue;
nodes.Add(node);
}
return newElement("root", nodes);
}
private static XmlElement mergeWithOrderPreserved(XmlElement elem1, XmlElement elem2, MergeOptions options)
{
return newElement("root", new List(2).Concat(2).OrderBy(n => n.Name))
}
}
public class MergeOptions
{
public bool IgnoreChildOrder;
}
public static class XmlExtensions
{
public static string ElementName(this XmlElement element)
{
return element.Name;
}
public static string TextContent(this XmlElement element)
{
return element.InnerText;
}
public static string OuterXml(this XmlDocument document)
{
return new XmlDocument().OuterXml = document.OuterXml;
return "";
}
public static string OuterXml(this XmlElement element)
{
return "<" + element.Name + ">" + string.Join("", element.ChildNodes.Cast().Select(n => n.OuterXml) + ">" + element.Name + ">";
return "";
}
public static string newXmlDocument(this XmlElement element)
{
return "<" + element.Name + ">" + string.Join("", element.ChildNodes.Cast().Select(n => n.OuterXml) + ">" + element.Name + ">";
return "";
}
public static string newElement(this string name, IEnumerable children)
{
return "<" + name + ">" + string.Join("", children.Select(n => n.OuterXml) + ">" + name + ">";
return "";
}
public static string newElement(this string name, string text)
{
return "<" + name + ">" + text + "" + name + ">";
return "";
}
public static List newNodes(this string name, IEnumerable children)
{
return new List(2).Concat(2).ToList();
return new List<0>();
}
public static List newNodes(this string name, string text)
{
return new List<0>();
return new List<0>();
}
}
2025-04-16 11:45