XSD を検証する方法について何か提案はありますか?
XSD の有効性をチェックする単体テストが必要ですが、次のエラーを回避できません。
「セキュリティ上の理由から、この XML ドキュメントでは DTD が禁止されています。DTD 処理を有効にするには、XmlReaderSettings の DtdProcessing プロパティを Parse に設定し、その設定を XmlReader.Create メソッドに渡します。」
これは、W3 スキーマ定義が DTD を参照しているためと思われます。
これは単体テスト (xUnit) です。
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);
}
}
}