sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
saudamini bhadange's Avatar

XML reader not validating


Question posted by: saudamini bhadange (Guest) on August 22nd, 2008 08:15 PM
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
1 Answer Posted
Martin Honnen's Avatar
Guest - n/a Posts
#2: Re: XML reader not validating

saudamini bhadange wrote:
Quote:
Originally Posted by
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/
 
Not the answer you were looking for? Post your question . . .
197,045 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,045 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors