Validate XDocument against schema without ValidationEventHandler (for use in the HTTP handler)

(I'm new to checking the circuit)

Regarding the following method

System.Xml.Schema.Extensions.Validate(
    ByVal source As System.Xml.Linq.XDocument, 
    ByVal schemas As System.Xml.Schema.XmlSchemaSet,
    ByVal validationEventHandler As System.Xml.Schema.ValidationEventHandler, 
    ByVal addSchemaInfo As Boolean)

I use it as follows inside IHttpHandler -

Try      
  Dim xsd As XmlReader = XmlReader.Create(context.Server.MapPath("~/App_Data/MySchema.xsd"))
  Dim schemas As New XmlSchemaSet() : schemas.Add("myNameSpace", xsd) : xsd.Close()
  myXDoxumentOdj.Validate(schemas, Function(s As Object, e As ValidationEventArgs) SchemaError(s, e, context), True)
Catch ex1 As Threading.ThreadAbortException
  'manage schema error'
  Return
Catch ex As Exception
  'manage other errors'
End Try

Handler -

  Function SchemaError(ByVal s As Object, ByVal e As ValidationEventArgs, ByVal c As HttpContext) As Object
    If c Is Nothing Then c = HttpContext.Current
    If c IsNot Nothing Then
      HttpContext.Current.Response.Write(e.Message)
      HttpContext.Current.Response.End()
    End If
    Return New Object()
  End Function

This works fine for me now, but it looks very weak. I get errors when I feed it with bad XML. But I want to implement it more elegantly. It looks like it will break for large XML, etc.

Is there a way to check without a handler so that I get a document confirmed at one time and then handle the errors?

For me, it looks like Async so that the call to Validate () will pass and some non-deterministic time later the handler will be called with the result / errors. Is it correct?

Thanks and sorry for any stupid mistakes :).

+3
1

, , Async. , .

+1

Source: https://habr.com/ru/post/1734539/


All Articles