-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlValidator.cs
110 lines (91 loc) · 3.21 KB
/
XmlValidator.cs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using Merthsoft.TokenIDE.Properties;
namespace XmlValidator {
/// <summary>
/// Handles setting up the XML libraries and validating documents
/// </summary>
public class XmlValidator {
List<ValidationError> errors = new List<ValidationError>();
int currentLine = -1;
int currentColumn = -1;
public IList<ValidationError> Errors {
get { return errors.AsReadOnly(); }
}
private void ValidationHandler(object sender, ValidationEventArgs args) {
errors.Add(new ValidationError(
currentLine + 1, // XmlReader numbers from line 0
currentColumn,
args.Message
));
}
/// <summary>
/// Validates an XML file using the given schema.
/// </summary>
/// <param name="schema">URI of the schema.</param>
/// <param name="xml">URI of the XML document.</param>
/// <returns><c>true</c> if validation is successful</returns>
public bool Validate(string schemaURI, string xmlURI) {
if (String.IsNullOrWhiteSpace(schemaURI)) {
errors.Add(new ValidationError(-1, -1, "Could not find schema"));
return false;
}
XmlReaderSettings s = new XmlReaderSettings();
s.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings | XmlSchemaValidationFlags.ProcessIdentityConstraints;
s.ValidationType = ValidationType.Schema;
s.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
s.Schemas.Add(null, schemaURI);
XmlReader reader = XmlReader.Create(xmlURI, s);
try {
while (reader.Read()) {
IXmlLineInfo lineInfo = reader as IXmlLineInfo;
currentLine = lineInfo.LineNumber;
currentColumn = lineInfo.LinePosition;
}
} catch (XmlException e) {
errors.Add(new ValidationError(e.LineNumber, e.LinePosition, e.Message));
}
reader.Close();
return errors.Count == 0;
}
public bool Validate(XmlSchema xmlSchema, string xmlURI) {
XmlReaderSettings s = new XmlReaderSettings();
s.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings | XmlSchemaValidationFlags.ProcessIdentityConstraints;
s.ValidationType = ValidationType.Schema;
s.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
//s.Schemas.Add(null, schemaURI);
s.Schemas.Add(xmlSchema);
XmlReader reader = XmlReader.Create(xmlURI, s);
try {
while (reader.Read()) {
IXmlLineInfo lineInfo = reader as IXmlLineInfo;
currentLine = lineInfo.LineNumber;
currentColumn = lineInfo.LinePosition;
}
} catch (XmlException e) {
errors.Add(new ValidationError(e.LineNumber, e.LinePosition, e.Message));
}
reader.Close();
return errors.Count == 0;
}
}
public class ValidationError {
int line;
int column;
string message;
public string Line { get { return line > 0 ? line.ToString() : String.Empty; } }
public string Column { get { return column > 0 ? column.ToString() : String.Empty; } }
public string Message { get { return message; } }
public ValidationError(int line, int column, string message) {
this.line = line;
this.column = column;
this.message = message;
}
public string[] ToArray() {
return new string[] { Line, Column, Message };
}
}
}