|
1
|
|
#region Copyright
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
#endregion
|
|
23
|
|
|
|
24
|
|
using System.Xml;
|
|
25
|
|
using System.IO;
|
|
26
|
|
using System.Xml.Schema;
|
|
27
|
|
using System.Reflection;
|
|
28
|
|
using log4net;
|
|
29
|
|
|
|
30
|
|
namespace ShowX.Model.XmlModel
|
|
31
|
|
{
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
public class XmlFileValidator : IXmlFileValidator
|
|
36
|
|
{
|
|
37
|
|
private static readonly ILog log = LogManager.GetLogger(
|
|
38
|
|
typeof(XmlFileValidator));
|
|
39
|
|
|
|
40
|
|
private bool validated = true;
|
|
41
|
|
|
|
42
|
|
#region IXmlValidator Members
|
|
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
46
|
public bool Validate(System.Xml.XmlNode node, string embeddedXsdRes)
|
|
51
|
|
{
|
|
52
|
46
|
log.Debug(string.Format("Validating Xml node against '{0}'",
|
|
53
|
|
embeddedXsdRes));
|
|
54
|
|
|
|
55
|
46
|
XmlValidatingReader vr = new XmlValidatingReader(node.OuterXml,
|
|
56
|
|
XmlNodeType.Element, new XmlParserContext(null,
|
|
57
|
|
new XmlNamespaceManager(new NameTable()),null,XmlSpace.None));
|
|
58
|
|
|
|
59
|
46
|
Assembly asm = Assembly.GetExecutingAssembly();
|
|
60
|
|
|
|
61
|
46
|
Stream embeddedXsdStream = asm.GetManifestResourceStream(
|
|
62
|
|
embeddedXsdRes);
|
|
63
|
|
|
|
64
|
46
|
XmlTextReader sr = new XmlTextReader(embeddedXsdStream);
|
|
65
|
|
|
|
66
|
46
|
ValidationEventHandler eHandler = new ValidationEventHandler(
|
|
67
|
|
XmlValidator_EventHandler);
|
|
68
|
|
|
|
69
|
46
|
XmlSchema schema = XmlSchema.Read(sr,eHandler);
|
|
70
|
|
|
|
71
|
46
|
vr.Schemas.Add(schema);
|
|
72
|
46
|
vr.ValidationEventHandler += eHandler;
|
|
73
|
|
|
|
74
|
46
|
vr.ValidationType = ValidationType.Schema;
|
|
75
|
|
|
|
76
|
931
|
while (vr.Read());
|
|
77
|
|
|
|
78
|
46
|
return validated;
|
|
79
|
|
}
|
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
|
|
86
|
4
|
public void XmlValidator_EventHandler(object sender, ValidationEventArgs e)
|
|
87
|
|
{
|
|
88
|
4
|
validated = false;
|
|
89
|
4
|
log.Fatal(string.Format("Validation error: {0}. Severity: {1}."
|
|
90
|
|
,e.Message,e.Severity));
|
|
91
|
|
}
|
|
92
|
|
#endregion
|
|
93
|
|
}
|
|
94
|
|
}
|
|
95
|
|
|