Connecting Tech Pros Worldwide Help | Site Map

XML reader not validating

  #1  
Old August 22nd, 2008, 08:15 PM
saudamini bhadange
Guest
 
Posts: n/a
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
  #2  
Old August 23rd, 2008, 12:35 PM
Martin Honnen
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
attribute not declared for validating schema init William Johnston answers 5 February 18th, 2007 01:05 PM
odd performance question - xml parsing Mark answers 2 January 18th, 2006 04:25 PM
XML Schema Validation + Deserialization Shone answers 2 November 12th, 2005 03:18 AM
bxmlnode: A C++ XML file reader (parser) Daniel Howard answers 0 July 20th, 2005 08:32 AM