Validating a XML input

T

Thiago Macedo

Hi again, folks.

i'm developing a windows application that gets data from an Mysql
database through PHP server-side scripts. I'm not totally familiar
with Requests and XML on .Net 2.0.
I'll load these sort of data - simply generated by the phps - into a
dataset and i want to validate, before, if the response from server
isn't anything other than an valid XML structure, to be able to catch
eventual server-side errors.
How this could be done? Load the data into a xml object and catch
parse errors?

Thanks for any appointments

Thiago
 
M

Martin Honnen

Thiago said:
i'm developing a windows application that gets data from an Mysql
database through PHP server-side scripts. I'm not totally familiar
with Requests and XML on .Net 2.0.
I'll load these sort of data - simply generated by the phps - into a
dataset and i want to validate, before, if the response from server
isn't anything other than an valid XML structure, to be able to catch
eventual server-side errors.
How this could be done? Load the data into a xml object and catch
parse errors?

I am not sure where XML comes in if PHP and MySQL generate data but
assuming you get XML and want to check it is well-formed then simply
pull it through an XmlReader:

Using reader As XmlReader = XmlReader.Create("file.xml")
Try
While reader.Read()
End While
Catch ex As XmlException
' handle error that XML is not well-formed
End Try
End Using

If you have an W3C XML Schema for the XML then you can also validate it
by using XmlReaderSettings where you set the ValidationType to Schema,
add any schemas you have to the Schemas property and set up a
ValidationEventHandler. See
http://msdn.microsoft.com/en-us/library/hdf992b8(VS.80).aspx
 
T

Thiago Macedo

I am not sure where XML comes in if PHP and MySQL generate data but
assuming you get XML and want to check it is well-formed then simply
pull it through an XmlReader:

Using reader As XmlReader = XmlReader.Create("file.xml")
Try
While reader.Read()
End While
Catch ex As XmlException
' handle error that XML is not well-formed
End Try
End Using

If you have an W3C XML Schema for the XML then you can also validate it
by using XmlReaderSettings where you set the ValidationType to Schema,
add any schemas you have to the Schemas property and set up a
ValidationEventHandler. Seehttp://msdn.microsoft.com/en-us/library/hdf992b8(VS.80).aspx

I'm converting the mysql data manualy by PHP script. So I have a
simple well-formed XML, with no schemas.
Ok, i can get the xml exception, but after read it (xmlreader) i lost
the stream and can't load the data into the dataset.
Another approaches? Am I missing something?

thank you for your help

Thiago
 
T

Thiago Macedo

Thiago,

\\\
Try
yourDataSet.ReadXML(path (or I thought just the serialized xml file)
Catch
'hello there is an error
End Try
///

http://msdn.microsoft.com/en-us/library/system.data.dataset.readxml(V...

(Don't become afraid for the samples, the above works in all normal
situations).

Cor

Hi Cor, just see your message.

Yes, that's not good samples.. but i already can load the data. My
problem is on handling the server-side errors, which is generated by
the PHPs (i'm calling them 'services').
I call them with an HttpWebRequest and input the data into the DataSet
with it response. But before i need to check if there aren't any php
messages, and in this case no xml would be generated, therefore the
"catch" statement
 
M

Martin Honnen

Thiago said:
I'm converting the mysql data manualy by PHP script. So I have a
simple well-formed XML, with no schemas.
Ok, i can get the xml exception, but after read it (xmlreader) i lost
the stream and can't load the data into the dataset.
Another approaches? Am I missing something?

If you want to pass the XmlReader to another method like the ReadXml
method of a DataSet then you don't need the while loop with the Read
call that I posted. If you get an XmlException then the markup is not
well-formed, I thought that is what you want to check when you asked for
"validating XML input".
 
T

Thiago Macedo

If you want to pass the XmlReader to another method like the ReadXml
method of a DataSet then you don't need the while loop with the Read
call that I posted. If you get an XmlException then the markup is not
well-formed, I thought that is what you want to check when you asked for
"validating XML input".

Yes, you get my point. But it only "validates" the input on the read
method, and the object is foward-only, so can't be read by ReadXml.
I may be missing something on the XmlReader class.. think this could
be done.

Thiago
 
M

Martin Honnen

Thiago said:
Yes, you get my point. But it only "validates" the input on the read
method, and the object is foward-only, so can't be read by ReadXml.
I may be missing something on the XmlReader class.. think this could
be done.

You have the choice: if you only want to check for well-formedness then
use an XmlReader with a while loop pulling in all nodes until you get an
exception or read through without getting an exception. If you
additionally want to load the XML into a DataSet then you need to use a
second reader. Or you do not set up an XmlReader at all and directly
load into the DataSet, that should nevertheless report any problems with
the XML.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top