Repository navigation
Expand file tree
/
Copy pathXmlDocumentLoader.cs
More file actions
73 lines (61 loc) · 2.34 KB
/
Copy pathXmlDocumentLoader.cs
File metadata and controls
73 lines (61 loc) · 2.34 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
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace BmwDeepObd
{
public class XmlDocumentLoader
{
// Public Static Methods
#region LoadWithIncludes(string filename)
public static XmlDocument LoadWithIncludes(string filename)
{
XmlDocument xml = new XmlDocument();
xml.Load(filename);
ProcessIncludeNodes(xml, xml.DocumentElement, Path.GetDirectoryName(filename));
return xml;
}
#endregion
// Private Static Methods
#region ProcessIncludeNodes(XmlDocument xml, XmlNode parent, string basePath)
public static void ProcessIncludeNodes(XmlDocument xml, XmlNode parent, string basePath)
{
List<XmlNode> removeList = new List<XmlNode>();
foreach (XmlNode node in parent.ChildNodes)
{
if (node.Name.ToLower() == "include" && node.Attributes != null)
{
string filename = node.Attributes["filename"].Value;
if (!Path.IsPathRooted(filename))
{
filename = Path.Combine(basePath, filename);
}
XmlDocument include = LoadWithIncludes(filename);
if (include?.DocumentElement != null)
{
foreach (XmlNode inner in include.DocumentElement.ChildNodes)
{
XmlNode imported = xml.ImportNode(inner, true);
if (imported.Attributes != null)
{
XmlAttribute fileNameAttr = xml.CreateAttribute("include_filename");
fileNameAttr.Value = filename;
imported.Attributes.Append(fileNameAttr);
}
parent.InsertBefore(imported, node);
}
}
removeList.Add(node);
}
else
{
ProcessIncludeNodes(xml, node, basePath);
}
}
foreach (XmlNode node in removeList)
{
parent.RemoveChild(node);
}
}
#endregion
}
}