-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlogMLResource.cs
More file actions
71 lines (61 loc) · 2.27 KB
/
BlogMLResource.cs
File metadata and controls
71 lines (61 loc) · 2.27 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
using System;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
namespace BlogML.Core
{
/// This is the original class using .net 1.1 XmlSchema class updated so API doens't change
/// BlogMLValidator is a new version of this class updated to use the Linq to xml classes
public class BlogMLResource
{
public static void Validate(XmlTextReader textReader) => Validate(textReader, null);
public static void Validate(XmlTextReader textReader, ValidationEventHandler validationEventHandler)
{
XmlReaderSettings validator = new XmlReaderSettings();
try
{
validator.Schemas.Add(GetSchema());
validator.ValidationType = ValidationType.Schema;
validator.ValidationEventHandler += validationEventHandler ?? new ValidationEventHandler(ValidationEvent);
XmlReader blog = XmlReader.Create(textReader, validator);
while (blog.Read()) {}
}
catch (Exception ex)
{
Console.WriteLine (ex.ToString());
}
}
public static void Validate(string inputUri) => Validate(inputUri, null);
public static void Validate(string inputUri, ValidationEventHandler validationEventHandler)
{
using (XmlTextReader reader = new XmlTextReader(inputUri))
{
Validate(reader, validationEventHandler);
}
}
private static void ValidationEvent(object sender, ValidationEventArgs e)
{
throw new InvalidOperationException(
string.Format("Validation {0} : {1}", e.Severity, e.Message)
);
}
public static XmlSchema GetSchema()
{
return XmlSchema.Read(
GetSchemaStream(),
new ValidationEventHandler(ValidationEvent)
);
}
public static Stream GetSchemaStream()
{
var assembly = Assembly.GetExecutingAssembly();
var resourceStream = assembly.GetManifestResourceStream("BlogML.Core.lib.blogml.xsd");
if (resourceStream == null)
{
throw new InvalidOperationException("Schema not found");
}
return resourceStream;
}
}
}