Validate XSD against W3 XMLSchema.xsd

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; text-align:left

Any suggestions on how I can validate my XSD?<br/>
<br/>
I would like to have a unit test that checks the validity of my XSD but I cant get past the following error:<br/>
<br/>
"For security reasons DTD is prohibited in this XML document. To enable DTD processing set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings into XmlReader.Create method." <br/>
<br/>
This seems to be because the W3 schema definition references DTDs.<br/>
<br/>
This is the unit test (xUnit):<br/>
<pre class="prettyprint namespace MyNamespace.Profile.Test
{
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

using Xunit;

public class ProfilesSchemaTests
{
[Fact]
public void ShouldValidateProfilesXsd()
{
string profilesXsd = "Profiles.xsd";
Assert.DoesNotThrow(() => ValidateXsd(profilesXsd));
}

private static void ValidateXsd(string path)
{
const string W3Schema = "http://www.w3.org/2001/XMLSchema.xsd";

var config = new XmlReaderSettings { ValidationType = ValidationType.Schema };
config.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
config.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
config.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
config.DtdProcessing = DtdProcessing.Parse;
config.XmlResolver = null;
config.ValidationEventHandler += ValidationCallBack;
config.Schemas.Add(null, W3Schema);

using (var reader = XmlReader.Create(path, config))
{
while (reader.Read())
{
}
}
}

private static void ValidationCallBack(object sender, ValidationEventArgs validationEventArgs)
{
Console.WriteLine(
validationEventArgs.Severity == XmlSeverityType.Warning
? "tWarning: Matching schema not found. No validation occurred. {0}"
: "tValidation error: {0}",
validationEventArgs.Message);
}
}
}
[/code]
<br/>
<br/>

View the full article
 
Back
Top