Connecting Tech Pros Worldwide Forums | Help | Site Map

XML reader not validating

saudamini bhadange
Guest
 
Posts: n/a
#1: Aug 22 '08
I have written folowing code for xml validation but it is not giving any errors for missing fields.
Even if the XML is in wrong format no error is thrown.

Here is the code...


Public Function validatexml(ByVal xml As String, ByVal operation As Integer) As Integer

Dim aTextReader As TextReader
aTextReader = New StringReader(xml)
Dim txtReader = XmlTextReader.Create(aTextReader)

bTextReader = New StreamReader("C:\\Schema\AddRequest.xsd")
Dim sc As New XmlSchema()
Dim schemareader As XmlReader = XmlReader.Create(bTextReader, Nothing)
sc = XmlSchema.Read(schemareader, AddressOf ValidationCallBack)

caseTextReader = New StreamReader("c:\\Schema\CaseRequest.xsd")
Dim caseSc As New XmlSchema()
Dim caseSchemareader As XmlReader = XmlReader.Create(caseTextReader)
caseSc = XmlSchema.Read(caseSchemareader, AddressOf ValidationCallBack)
caseSc.Parent = sc

Dim settings As New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema settings.IgnoreWhitespace = True
settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema
settings.ValidationFlags = XmlSchemaValidationFlags.AllowXmlAttributes
settings.NameTable = New System.Xml.NameTable()

settings.Schemas.Add(sc)
settings.Schemas.Add(caseSc)

AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack

Dim Reader As XmlReader = XmlReader.Create(txtReader, settings)
While Reader.Read()
Reader.Name.ToString()
End While
End Function

Private Sub ValidationCallBack(ByVal sender As Object, ByVal args As ValidationEventArgs)
'catch the error messages and count
p_Success = p_Success + 1
End Sub
Martin Honnen
Guest
 
Posts: n/a
#2: Aug 23 '08

re: XML reader not validating


saudamini bhadange wrote:
Quote:
I have written folowing code for xml validation but it is not giving any errors for missing fields.
Even if the XML is in wrong format no error is thrown.
You have set up a ValidationEventHandler thus any validation failure
will not result in error being thrown but rather in calling that
handler. As that handler does not output anything but simply increments
a variable I am not surprised that you don't see any errors. You need to
output args.Message in the handler.

If you still have problems then we need to see your XML and the schema(s).




--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Closed Thread