Created
January 6, 2016 11:15
-
-
Save miklund/4bd12d0696284edc4d8a to your computer and use it in GitHub Desktop.
2011-04-07 Forwarding events in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Title: Forwarding events in C# | |
# Author: Mikael Lundin | |
# Link: http://blog.mikaellundin.name/2011/04/07/forwarding-events-in-csharp.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HtmlValidator | |
{ | |
private readonly string html; | |
private XmlReaderSettings xmlValidationSettings; | |
public HtmlValidator(string @namespace, string schemaPath) | |
{ | |
this.html = html; | |
/* Load the schema */ | |
var xmlSchema = XmlReader.Create(schemaPath); | |
xmlValidationSettings = new XmlReaderSettings(); | |
xmlValidationSettings.Schemas.Add(@namespace, xmlSchema); | |
xmlValidationSettings.ValidationType = ValidationType.Schema; | |
} | |
public void Validate(string html) | |
{ | |
var htmlReader = new StringReader(html); | |
// Trigger this event on schema validation errors | |
// xmlValidationSettings.ValidationEventHandler += HandleValidationErrors; | |
// Read and validate htmlOutputStream | |
var xmlValidatingReader = XmlReader.Create(htmlReader, xmlValidationSettings); | |
while (xmlValidatingReader.Read()) ; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public event ValidationEventHandler OnValidationError | |
{ | |
add { xmlValidationSettings.ValidationEventHandler += value; } | |
remove { xmlValidationSettings.ValidationEventHandler -= value; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class Test | |
{ | |
private int CountHtmlErrors(string html) | |
{ | |
// Create validator | |
var validator = new HtmlValidator("urn:unordered-list", "UlLiSchema.xsd"); | |
// Add event handler | |
var errors = 0; | |
validator.OnValidationError += (o, e) => errors++; | |
// Validate html | |
validator.Validate(html); | |
return errors; | |
} | |
[Test] | |
public void HtmlShouldBeValid() | |
{ | |
var html = @"<ul xmlns=""urn:unordered-list""><li></li><li></li></ul>"; | |
// Assert the result | |
Assert.That(this.CountHtmlErrors(html), Is.EqualTo(0)); | |
} | |
[Test] | |
public void HtmlShouldBeInvalid() | |
{ | |
var html = @"<ul xmlns=""urn:unordered-list""><p>Hello</p></ul>"; | |
// Assert the result | |
Assert.That(this.CountHtmlErrors(html), Is.EqualTo(1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment