2012-05-03 115 views
0

关于如何验证我的XSD的任何建议?针对W3验证XSD XMLSchema.xsd

我想有一个单元测试,检查我的XSD的有效性,但我不能让过去的以下错误:

"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."

这似乎是因为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); 
     } 
    } 
} 

回答

0

在上MSDN forums我有一个非常好的建议 - 使用XmlSchemaSet.Compile因为这验证。