2013-08-26 120 views
0
  1. 我收到錯誤XML驗證錯誤:驗證對空模式設置

    The XmlSchemaSet on the document is either null or has no schemas in it. Provide schema information before calling Validate.

    但我所在的位置中的架構文件中指定:C:\InvSchema.xsd

    有沒有通過特定的命名空間,所以我把它設置爲空。

    那麼,爲什麼我會得到這個錯誤?

  2. 我還需要知道xml模式是否已成功驗證,但驗證函數沒有返回確認它的布爾值。

    如何從InvImp.Validate(evtHandler)獲得該值?

下面是代碼:

Private Function ValidateSchema() As Boolean 
      Try 
       Dim settings As XmlReaderSettings = New XmlReaderSettings() 
       settings.Schemas.Add("", "C:\InvSchema.xsd") 
       settings.ValidationType = ValidationType.Schema 
       Dim evtHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler) 
       Dim reader As XmlReader = XmlReader.Create(_fileName, settings) 
       Dim InvImp As XmlDocument = New XmlDocument 
       InvImp.Validate(evtHandler)    
       ValidateSchema = True 
      Catch ex As Exception 
       Throw ex 
       ValidateSchema = False 
      End Try 
     End Function 


Shared Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs) 
     Select Case e.Severity 
      Case XmlSeverityType.Error 
       Library.Log("Schema validation failed error " & e.Message, LogType.ERRORLOG, ImportType.InvcIMP) 
      Case XmlSeverityType.Warning 
       Library.Log("Schema validation warning " & e.Message, LogType.EVENTLOG, ImportType.InvcIMP) 
     End Select    
    End Sub 

回答

0

我覺得有兩個問題。該事件處理是沒有得到正確添加和XmlReader中應該用於驗證,而不是一個單獨的XmlDocument對象:

Private Function ValidateSchema() As Boolean 
    Try 
     Dim settings As XmlReaderSettings = New XmlReaderSettings() 
     settings.Schemas.Add("", "C:\InvSchema.xsd") 
     settings.ValidationType = ValidationType.Schema 
     AddHandler settings.ValidationEventHandler, AddressOf ValidationEventHandler 
     Dim reader As XmlReader = XmlReader.Create(_fileName, settings) 

     Do While reader.Read 
      'Triggers validation' 
     Loop 

    Catch ex As Exception 
     Throw ex 
    End Try 
    Return _valid 
End Function 

你也應該設定在你的事件處理程序的成員級別布爾(_valid)。事件處理程序將被調用以查找發現的每個模式違例。