Skip to content

Instantly share code, notes, and snippets.

@pisceanfoot
Created May 14, 2014 01:17
Show Gist options
  • Save pisceanfoot/15aa9b6e081a89c7a63a to your computer and use it in GitHub Desktop.
Save pisceanfoot/15aa9b6e081a89c7a63a to your computer and use it in GitHub Desktop.
merge multi xml file to xml
/// <summary>
///
/// </summary>
public static class XmlUtils
{
/// <summary>
/// MergeFiles to a single file
/// </summary>
/// <param name="files"></param>
/// <param name="fileContentMergeFail"></param>
/// <returns></returns>
public static string MergeFiles(List<string> files)
{
XmlDocument doc = new XmlDocument();
if (files.Count > 0)
{
string fileToMerge = null;
for (int i = 0; i < files.Count; i++)
{
fileToMerge = files[i];
if (!File.Exists(fileToMerge))
{
throw new FileNotFoundException(fileToMerge);
}
if (doc.DocumentElement == null)
{
doc.Load(fileToMerge);
}
else
{
XmlDocument docTemp = new XmlDocument();
docTemp.Load(fileToMerge);
foreach (XmlNode node in docTemp.DocumentElement.ChildNodes)
{
XmlNode nodeTemp = doc.ImportNode(node, true);
doc.DocumentElement.AppendChild(nodeTemp);
}
}
}
}
return doc.OuterXml;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment