Validation against XSD with different namespace

Unanswered Validation against XSD with different namespace

  • 2012年6月19日 下午 04:42
     
      包含代碼

    I have an XML file and an XSD file that may have the wrong namespace if someone has put the wrong version in the validation folder.  When the file is the correct version, then the validator works fine, and lists all errors in the XML, but when wrong, just reports the XML as valid.

    I'm using the following code..

    Dim errors As Boolean = False
    Dim FullErrors As String = ""
    Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
        FullErrors += Regex.Replace(e.Message, "http:.*:", "") + Environment.NewLine
        errors = True
    End Sub
    
    Private Sub btnValidateXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValidateXML.Click
        Dim schemas As XmlSchemaSet = New XmlSchemaSet()
        Dim XMLDoc As New XDocument()
    
        Try
            XMLDoc = XDocument.Parse(txtXML.Text)
        Catch ex As Exception
            txtXMLErrors.Text = "This is not a well formed XML document"
            Return
        End Try
        schemas.Add(Nothing, "exb.xsd")
    
        errors = False
        FullErrors = ""
        XMLDoc.Validate(schemas, AddressOf XSDErrors)
    
        If errors Then
            txtXMLErrors.Text = FullErrors
        Else
            txtXMLErrors.Text = "Validation Successful"
        End If
    End Sub
    

    How can you check that the namespace of the XML and XSD documents match before doing the validation?