-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXmlComplexer.cs
More file actions
90 lines (84 loc) · 3.21 KB
/
XmlComplexer.cs
File metadata and controls
90 lines (84 loc) · 3.21 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
89
90
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
namespace XmlComplex
{
/// <summary>
/// Combine XML document helper
/// </summary>
class XmlComplexer
{
/// <summary>
/// Combine XML document
/// </summary>
/// <param name="_items">Merge files</param>
/// <param name="_baseFile">Base file</param>
/// <returns>Merged XML document</returns>
static public XmlDocument Combine(string _baseFile, params string[] _items)
{
var _basedoc = new XmlDocument();
_basedoc.Load(_baseFile);
foreach (var _item in _items)
{
var _xml = new XmlDocument();
_xml.Load(_item);
proc(_basedoc.ChildNodes[1] as XmlElement, _xml.ChildNodes[1] as XmlElement);
}
return _basedoc;
}
/// <summary>
/// Combine xml document
/// </summary>
/// <param name="basedata">Base XML document</param>
/// <param name="importdata">Merge XML document</param>
static void proc(XmlElement basedata, XmlElement importdata)
{
if (basedata == null || importdata == null)
{
return;
}
if (!isSameElement(basedata, importdata))
{
basedata.InnerXml += importdata.OuterXml;
return;
}
//Enumurate elements
foreach (var element in importdata.ChildNodes.Cast<object>().Where(w=>w is XmlElement).Cast<XmlElement>().Where(w=>w!= null))
{
var sameelement = basedata.ChildNodes
.Cast<object>().Where(w => w is XmlElement).Cast<XmlElement>()
.Where(w => w != null)
.Where(searchel => isSameElement(element, searchel))
;
//Recursive call for merge elements
foreach(var searchel in sameelement)
proc(searchel, element);
if (!sameelement.Any())
basedata.InnerXml += element.OuterXml;
}
}
/// <summary>
/// Check XML element is same (all element attributes are equal)
/// </summary>
/// <param name="basedata">Base element</param>
/// <param name="importdata">Target element</param>
/// <returns>is same</returns>
static bool isSameElement(XmlElement basedata, XmlElement importdata)
{
if (basedata.Name != importdata.Name || basedata.Attributes.Count != importdata.Attributes.Count)
return false;
return basedata.Attributes
.Cast<XmlAttribute>()
.All(_attr =>
importdata.Attributes
.Cast<XmlAttribute>()
.Any(_check =>
_attr.Name.Equals(_check.Name) &&
_attr.Value == _check.Value
)
);
}
}
}