473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Verify an xml file is valid

Hi;

What's the fastest way to verify that an xml file is well-formed (and valid
if it has a dtd)? I don't want to pull any data out, I just want to make sure
it is a good file. (When the user selects an xml file to save and use in the
future, we want to do a fast sanity check on it.)

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Mar 9 '06 #1
9 5669
David Thielen wrote:
Hi;

What's the fastest way to verify that an xml file is well-formed (and valid
if it has a dtd)? I don't want to pull any data out, I just want to make sure
it is a good file. (When the user selects an xml file to save and use in the
future, we want to do a fast sanity check on it.)


Just run a parser/validator over it.
See the FAQ: http://xml.silmaril.ie/authors/parsers/

///Peter
Mar 9 '06 #2
Hi;

I was thinking more in terms of a .NET class which I could use to open and
read the file? Having an external program that must be installed correctly is
just one more thing that can go wrong.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Peter Flynn" wrote:
David Thielen wrote:
Hi;

What's the fastest way to verify that an xml file is well-formed (and valid
if it has a dtd)? I don't want to pull any data out, I just want to make sure
it is a good file. (When the user selects an xml file to save and use in the
future, we want to do a fast sanity check on it.)


Just run a parser/validator over it.
See the FAQ: http://xml.silmaril.ie/authors/parsers/

///Peter

Mar 10 '06 #3
Hi Dave,

Try to load the xml file into XmlDocument class using the Load or LoadXml
method. If it is not well-formed, an exception will be thrown.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Mar 10 '06 #4
Kevin - that is way too easy <g>.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Kevin Yu [MSFT]" wrote:
Hi Dave,

Try to load the xml file into XmlDocument class using the Load or LoadXml
method. If it is not well-formed, an exception will be thrown.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Mar 10 '06 #5


David Thielen wrote:

What's the fastest way to verify that an xml file is well-formed (and valid
if it has a dtd)? I don't want to pull any data out, I just want to make sure
it is a good file.


With .NET 1.x you should use an XmlValidatingReader then (if you want to
validate against a DTD, otherwise an XmlTextReader suffices to check
well-formedness). The validating reader would be used to read through
the complete document (e.g. while (reader.Read()) {}) and your
ValidationEventHandler would check for any errors being reported.
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlValidatingReaderClassTopic.asp>
With .NET 2.0 you need to use the right settings to get an XmlReader
doing the validation. Let us know if you want to use .NET 2.0 but can't
find the right settings for DTD validation.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 10 '06 #6
Hello;

Yes it is .NET 2.0. And I realized last night that XmlDocument is creating a
DOM and I'd prefer to avoid that overhead. I just want it to read through and
then discard the document so a SAX reader is fine.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Martin Honnen" wrote:


David Thielen wrote:

What's the fastest way to verify that an xml file is well-formed (and valid
if it has a dtd)? I don't want to pull any data out, I just want to make sure
it is a good file.


With .NET 1.x you should use an XmlValidatingReader then (if you want to
validate against a DTD, otherwise an XmlTextReader suffices to check
well-formedness). The validating reader would be used to read through
the complete document (e.g. while (reader.Read()) {}) and your
ValidationEventHandler would check for any errors being reported.
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlValidatingReaderClassTopic.asp>
With .NET 2.0 you need to use the right settings to get an XmlReader
doing the validation. Let us know if you want to use .NET 2.0 but can't
find the right settings for DTD validation.
--

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

Mar 10 '06 #7


David Thielen wrote:
Yes it is .NET 2.0. And I realized last night that XmlDocument is creating a
DOM and I'd prefer to avoid that overhead. I just want it to read through and
then discard the document so a SAX reader is fine.


..NET has no SAX push parser but has a pull parser with XmlReader, here
is an example on how to that with .NET 2.0 if you want to validate
against a DTD, the example aborts parsing if an error is found:

XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ProhibitDtd = false;
readerSettings.ValidationType = ValidationType.DTD;
XmlReader xmlReader = XmlReader.Create(@"example.xml", readerSettings);
bool valid = true;
bool moreNodes = true;
do {
try {
moreNodes = xmlReader.Read();
}
catch (XmlException e) {
valid = false;
Console.WriteLine(
"Parse error \"{0}\" at line {1}, position {2}.", e.Message,
e.LineNumber, e.LinePosition);
}
catch (XmlSchemaValidationException se) {
valid = false;
Console.WriteLine(
"Validation error \"{0}\" at line {1}, position {2}.", se.Message,
se.LineNumber, se.LinePosition);
}
}
while (valid && moreNodes);
if (valid) {
Console.WriteLine("No errors found during XML parsing.");
}
Alternatively if you wanted to continue parsing to report all errors you
need a ValidationEventHandler, see the documentation of the
XmlReaderSettings class in your SDK documentation or online here:
<http://msdn2.microsoft.com/en-us/library/system.xml.xmlreadersettings(VS.80).aspx>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 10 '06 #8
perfect - thanks

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Martin Honnen" wrote:


David Thielen wrote:
Yes it is .NET 2.0. And I realized last night that XmlDocument is creating a
DOM and I'd prefer to avoid that overhead. I just want it to read through and
then discard the document so a SAX reader is fine.


..NET has no SAX push parser but has a pull parser with XmlReader, here
is an example on how to that with .NET 2.0 if you want to validate
against a DTD, the example aborts parsing if an error is found:

XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ProhibitDtd = false;
readerSettings.ValidationType = ValidationType.DTD;
XmlReader xmlReader = XmlReader.Create(@"example.xml", readerSettings);
bool valid = true;
bool moreNodes = true;
do {
try {
moreNodes = xmlReader.Read();
}
catch (XmlException e) {
valid = false;
Console.WriteLine(
"Parse error \"{0}\" at line {1}, position {2}.", e.Message,
e.LineNumber, e.LinePosition);
}
catch (XmlSchemaValidationException se) {
valid = false;
Console.WriteLine(
"Validation error \"{0}\" at line {1}, position {2}.", se.Message,
se.LineNumber, se.LinePosition);
}
}
while (valid && moreNodes);
if (valid) {
Console.WriteLine("No errors found during XML parsing.");
}
Alternatively if you wanted to continue parsing to report all errors you
need a ValidationEventHandler, see the documentation of the
XmlReaderSettings class in your SDK documentation or online here:
<http://msdn2.microsoft.com/en-us/library/system.xml.xmlreadersettings(VS.80).aspx>

--

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

Mar 11 '06 #9
David,

The fastest way to verify if a file is valid is by using XmlReader with
XmlReaderSettings set to validate.

See the following MSDN link to validating readers:
http://msdn2.microsoft.com/en-us/lib...b8(VS.80).aspx

If you are using .NET 1.1, please refer to the XmlValidatingReader
documentation.

The XmlDocument load method will just do well-formedness checking, it will
not check DTD content model constraints.

Thanks,
Amol

"David Thielen" <th*****@nospam.nospam> wrote in message
news:56**********************************@microsof t.com...
Kevin - that is way too easy <g>.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Kevin Yu [MSFT]" wrote:
Hi Dave,

Try to load the xml file into XmlDocument class using the Load or LoadXml
method. If it is not well-formed, an exception will be thrown.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Apr 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Support | last post by:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head>...
2
by: MrMike | last post by:
This is a tough one, but here goes. I have a webform textbox where users input a filepath. For example: \\servername\sharename\file.xls. Is there anyway that I could use VB code to verify that...
2
by: Wayne Wengert | last post by:
I want to write a Windows application to go through all the email addresses in an SQL Server table and to report which ones are invalid. From Googling and perusing NGs it is my understanding that...
43
by: Xancatal | last post by:
Hey everybody. I need help on this one. I need to verify that a number entered by a user is not either a negative number (-100.00), or an alphabet (a, b, c, X, Y) as well as other number other than...
1
by: dotnet | last post by:
Is there a way to validate if a CSV file is Valid in dotnet? Any direction is appriciate it.
14
by: =?Utf-8?B?U2FtdWVs?= | last post by:
Hi, I have a web app that allows others to upload files, and the problem is that if I allow users to upload image files, fake image can be uploaded and cause XSS issues. In the app, I do...
3
by: Grey | last post by:
I have to write a program to verify email address availability. i have to verify thousand of email address. is there any way to verify the email in ..net instead. the requirement is to verify which...
1
by: pnsreee | last post by:
Hi I have following strings Sun:mon:tue mon:tue:wed mon:wed:mon I need to check the string have valid values sun,mon,tue,wed and also check duplicates are there or not
1
by: rcamarda | last post by:
I have a database backup that our vendor proformed. I dont know if they verified the backup, nor do I have the disk space to create a new backup with verify or retore the current backup. Is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.