2009-07-02 31 views
0

メモリ内のXMLスキーマを検証したいときにこの奇妙なエラーが発生しました。XMLDocument.Validateエラー:System.InvalidOperationException:ドキュメントのXmlSchemaSetがnullかスキーマがありません

[Test] 
    public void ValidationXML() 
    { 
     int errorCount = 0; 
     var xmlString = "<?xml version='1.0'?><response><error code='1'> Success</error></response>"; 
     XmlDocument xmlDocument = new XmlDocument(); 
     xmlDocument.LoadXml(xmlString); 
     xmlDocument.Validate((sender, e) => errorCount++); 
     Assert.AreEqual(0, errorCount); 
    } 

例外は次のとおり

failed: System.InvalidOperationException : The XmlSchemaSet on the document is either null or has no schemas in it. Provide schema information before calling Validate. 
    at System.Xml.XmlDocument.Validate(ValidationEventHandler validationEventHandler, XmlNode nodeToValidate) 
    at System.Xml.XmlDocument.Validate(ValidationEventHandler validationEventHandler) 

答えて

3

あなたは、チェック対象のスキーマを割り当てずに、常にXmlDocumentを検証しようとしています。

xmlDocument.Schemas.Add(new XmlSchema()); 

これは空のスキーマに対して検証を試みる(ヌルとは対照的に)と(代わりに例外をスローする)検証に失敗し、1

にERRORCOUNT設定
関連する問題