1

XPathNavigator.CheckValidityXMLドキュメントの検証に使用しようとしています。どういうわけか、私はこの方法を使用して合格したテストを書くことができましたが、今では(不思議なことに)もう合格していません。変更されたのは.NET2から.NET3.5に移行したことだけですが、その移行中に変更されたドキュメントは見つかりません。

プログラムの例を次に示します。

void Main()
{
    try
    {
        GetManifest().CreateNavigator().CheckValidity(GetSchemaSet(), (sender, args) => {
            // never get in here when debugging
            if (args.Severity == XmlSeverityType.Error) {
                throw new XmlSchemaValidationException("Manifest failed validation", args.Exception);
            }
        }); // returns true when debugging
    }
    catch (XmlSchemaValidationException)
    {
        // never get here
        throw;
    }

    // code here runs
}

IXPathNavigable GetManifest()
{
    using (TextReader manifestReader = new StringReader("<?xml version='1.0' encoding='utf-8' ?><BadManifest><bad>b</bad></BadManifest>"))
    {
        return new XPathDocument(manifestReader);
    }
}

XmlSchemaSet GetSchemaSet() 
{
    var schemaSet = new XmlSchemaSet();
    using (var schemaReader = new StringReader(Schema)){
        schemaSet.Add(XmlSchema.Read(schemaReader, null));
    }

    return schemaSet;
}

const string Schema = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<xs:schema attributeFormDefault=""unqualified"" elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""http://www.engagesoftware.com/Schemas/EngageManifest"">
  <xs:element name=""EngageManifest"">
    <xs:complexType>
      <xs:all>
        <xs:element name=""Title"" type=""xs:string"" />
        <xs:element name=""Description"" type=""xs:string"" />
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>";

C#を使用してXMLを変更せずにXSDスキーマでXMLを検証するソリューションを試しましたが、同じ結果が得られています...この検証がどのように機能するかについて、いくつかの大きな考慮事項を見逃している必要がありますが、できません見て...

4

1 に答える 1

3

問題は、XMLがデフォルトの名前空間を使用しているが、XSDがターゲットの名前空間を指定していることです。XMLで指定する<BadManifest xmlns="http://www.engagesoftware.com/Schemas/EngageManifest">と、バリデーターが期待どおりにエラーを報告することがわかります。それ以外の場合は、XMLの名前空間を認識しないため、無視します。

于 2010-12-06T18:24:30.770 に答える