Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 9th, 2006, 09:35 PM
David Thielen
Guest
 
Posts: n/a
Default 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

  #2  
Old March 9th, 2006, 09:45 PM
Peter Flynn
Guest
 
Posts: n/a
Default Re: Verify an xml file is valid

David Thielen wrote:[color=blue]
> 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.)[/color]

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

///Peter
  #3  
Old March 10th, 2006, 01:15 AM
David Thielen
Guest
 
Posts: n/a
Default Re: Verify an xml file is valid

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:
[color=blue]
> David Thielen wrote:[color=green]
> > 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.)[/color]
>
> Just run a parser/validator over it.
> See the FAQ: http://xml.silmaril.ie/authors/parsers/
>
> ///Peter
>[/color]
  #4  
Old March 10th, 2006, 03:25 AM
Kevin Yu [MSFT]
Guest
 
Posts: n/a
Default Re: Verify an xml file is valid

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

  #5  
Old March 10th, 2006, 04:25 AM
David Thielen
Guest
 
Posts: n/a
Default Re: Verify an xml file is valid

Kevin - that is way too easy <g>.

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



"Kevin Yu [MSFT]" wrote:
[color=blue]
> 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."
>
>[/color]
  #6  
Old March 10th, 2006, 01:26 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Verify an xml file is valid



David Thielen wrote:

[color=blue]
> 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.[/color]

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/
  #7  
Old March 10th, 2006, 02:16 PM
David Thielen
Guest
 
Posts: n/a
Default Re: Verify an xml file is valid

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:
[color=blue]
>
>
> David Thielen wrote:
>
>[color=green]
> > 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.[/color]
>
> 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/
>[/color]
  #8  
Old March 10th, 2006, 02:55 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Verify an xml file is valid



David Thielen wrote:
[color=blue]
> 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.[/color]

..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/
  #9  
Old March 11th, 2006, 09:55 PM
David Thielen
Guest
 
Posts: n/a
Default Re: Verify an xml file is valid

perfect - thanks

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



"Martin Honnen" wrote:
[color=blue]
>
>
> David Thielen wrote:
>[color=green]
> > 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.[/color]
>
> ..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/
>[/color]
  #10  
Old April 11th, 2006, 07:45 PM
Amol Kher [MSFT]
Guest
 
Posts: n/a
Default Re: Verify an xml file is valid

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" <thielen@nospam.nospam> wrote in message
news:5672E395-D957-4D15-80DA-2E6070A37EDC@microsoft.com...[color=blue]
> Kevin - that is way too easy <g>.
>
> --
> thanks - dave
> david_at_windward_dot_net
> http://www.windwardreports.com
>
>
>
> "Kevin Yu [MSFT]" wrote:
>[color=green]
>> 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."
>>
>>[/color][/color]


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles