Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Validating a XML input

5 views
Skip to first unread message

Thiago Macedo

unread,
May 1, 2008, 12:08:23 AM5/1/08
to
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

Martin Honnen

unread,
May 1, 2008, 7:52:33 AM5/1/08
to
Thiago Macedo wrote:

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

--

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

Cor Ligthert[MVP]

unread,
May 1, 2008, 8:58:41 AM5/1/08
to
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(VS.71).aspx

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

Cor

"Thiago Macedo" <thiago...@gmail.com> schreef in bericht
news:06fbe77f-f71d-48a5...@a1g2000hsb.googlegroups.com...

Thiago Macedo

unread,
May 1, 2008, 9:32:53 AM5/1/08
to
> ValidationEventHandler. Seehttp://msdn.microsoft.com/en-us/library/hdf992b8(VS.80).aspx

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

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

Thiago Macedo

unread,
May 1, 2008, 10:01:32 AM5/1/08
to
On May 1, 9:58 am, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:

> 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
>
> "Thiago Macedo" <thiago.ch...@gmail.com> schreef in berichtnews:06fbe77f-f71d-48a5...@a1g2000hsb.googlegroups.com...

>
> > 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

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

Martin Honnen

unread,
May 1, 2008, 12:18:28 PM5/1/08
to
Thiago Macedo wrote:

> 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".

Thiago Macedo

unread,
May 1, 2008, 1:13:28 PM5/1/08
to

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

Martin Honnen

unread,
May 2, 2008, 6:50:58 AM5/2/08
to
Thiago Macedo wrote:

> 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.

0 new messages