-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
88 lines (79 loc) · 2.24 KB
/
Program.cs
File metadata and controls
88 lines (79 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace XmlMerger
{
internal static class Program
{
private static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Please provide a folder and an output filename as command line arguments.");
Console.WriteLine("Like: XmlMerger . output.xml");
return;
}
string path = GetAbsolutePath(null, args[0]);
string[] files;
Console.WriteLine($"Trying path: {path}.");
if (Directory.Exists(path))
{
files = Directory.GetFiles(path, "*.xml");
if (files.Length == 0)
{
Console.WriteLine("No XML files have been found.");
return;
}
}
else
{
Console.WriteLine("Path not found.");
return;
}
var xmlDocuments = new List<XmlDocument>();
foreach (string file in files)
{
var doc = new XmlDocument();
doc.Load(file);
xmlDocuments.Add(doc);
}
for (int i = xmlDocuments.Count - 1; i >= 1; i--)
{
XmlDocument docA = xmlDocuments[i - 1];
XmlDocument docB = xmlDocuments[i];
foreach (XmlNode childEl in docB.DocumentElement.ChildNodes)
{
XmlNode newNode = docA.ImportNode(childEl, true);
docA.DocumentElement.AppendChild(newNode);
}
}
Console.WriteLine($"Writing to {args[1]} in current directory.");
xmlDocuments[0].Save(args[1]);
}
private static string GetAbsolutePath(string basePath, string path)
{
if (path == null)
return null;
if (basePath == null)
basePath = Path.GetFullPath("."); // quick way of getting current working directory
else
basePath = GetAbsolutePath(null, basePath); // to be REALLY sure ;)
string finalPath;
// specific for windows paths starting on \ - they need the drive added to them.
// I constructed this piece like this for possible Mono support.
if (!Path.IsPathRooted(path) || "\\".Equals(Path.GetPathRoot(path)))
{
finalPath = path.StartsWith(Path.DirectorySeparatorChar.ToString())
? Path.Combine(Path.GetPathRoot(basePath), path.TrimStart(Path.DirectorySeparatorChar))
: Path.Combine(basePath, path);
}
else
{
finalPath = path;
}
// resolves any internal "..\" to get the true full path.
return Path.GetFullPath(finalPath);
}
}
}