473,654 Members | 3,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_windwa rd_dot_net
http://www.windwardreports.com

Mar 9 '06 #1
9 5686
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_windwa rd_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_windwa rd_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 XmlValidatingRe ader 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
ValidationEvent Handler would check for any errors being reported.
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX mlValidatingRea derClassTopic.a sp>
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_windwa rd_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 XmlValidatingRe ader 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
ValidationEvent Handler would check for any errors being reported.
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX mlValidatingRea derClassTopic.a sp>
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:

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

--

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

--
thanks - dave
david_at_windwa rd_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:

XmlReaderSettin gs readerSettings = new XmlReaderSettin gs();
readerSettings. ProhibitDtd = false;
readerSettings. ValidationType = ValidationType. DTD;
XmlReader xmlReader = XmlReader.Creat e(@"example.xml ", readerSettings) ;
bool valid = true;
bool moreNodes = true;
do {
try {
moreNodes = xmlReader.Read( );
}
catch (XmlException e) {
valid = false;
Console.WriteLi ne(
"Parse error \"{0}\" at line {1}, position {2}.", e.Message,
e.LineNumber, e.LinePosition) ;
}
catch (XmlSchemaValid ationException se) {
valid = false;
Console.WriteLi ne(
"Validation error \"{0}\" at line {1}, position {2}.", se.Message,
se.LineNumber, se.LinePosition );
}
}
while (valid && moreNodes);
if (valid) {
Console.WriteLi ne("No errors found during XML parsing.");
}
Alternatively if you wanted to continue parsing to report all errors you
need a ValidationEvent Handler, see the documentation of the
XmlReaderSettin gs class in your SDK documentation or online here:
<http://msdn2.microsoft .com/en-us/library/system.xml.xmlr eadersettings(V S.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
XmlReaderSettin gs 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 XmlValidatingRe ader
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******** *************** ***********@mic rosoft.com...
Kevin - that is way too easy <g>.

--
thanks - dave
david_at_windwa rd_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
1751
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> <meta http-equiv=Content-Type content="text/html; charset=windows-1252"> <meta name=ProgId content=Word.Document> <meta name=Generator content="Microsoft Word 11"> <meta name=Originator content="Microsoft Word 11">
2
4502
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 the filepath and filename are valid after the user has input them? Basically this would serve to quality check whether or not the user has input the correct file path. Thanks.
2
5260
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 the process to validate an email address is done at 3 levels: 1. Verify that it is syntactically valid 2. Verify that the domain exists (SMTP verification) 3. Verify that the email address exists at that domain (MX verification) The first one I...
43
6560
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 positive integers or a decimal point. For example: Enter amount: and was capturing the float varialbe as in: scanf ("%f", &myVar)
1
1319
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
2791
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 check image dimension when uploaded so that any fake image that is actually a text file is blocked (user renames a .txt to .gif, e.g.). However, a png file renamed to .gif can contain script that when loaded
3
1812
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 email valid or invalid
1
3499
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
6254
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 there a t-sql command that I can use to verify the database backup after the fact? SQL Server 2000 currenlty patched The backup of the database is a 97GB file on the server in question. TIA Rob
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.