Home All Groups Group Topic Archive Search About

Verify an xml file is valid

Author
9 Mar 2006 9:25 PM
David Thielen
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

Author
9 Mar 2006 9:31 PM
Peter Flynn
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
Author
10 Mar 2006 1:05 AM
David Thielen
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



Show quote
"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
>
Author
10 Mar 2006 3:10 AM
Kevin Yu [MSFT]
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."
Author
10 Mar 2006 4:09 AM
David Thielen
Kevin - that is way too easy <g>.

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



Show quote
"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."
>
>
Author
11 Apr 2006 6:33 PM
Amol Kher [MSFT]
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/library/hdf992b8(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

Show quote
"David Thielen" <thielen@nospam.nospam> wrote in message
news:5672E395-D957-4D15-80DA-2E6070A37EDC@microsoft.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."
>>
>>
Author
10 Mar 2006 1:15 PM
Martin Honnen
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/
Author
10 Mar 2006 2:00 PM
David Thielen
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



Show quote
"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/
>
Author
10 Mar 2006 2:38 PM
Martin Honnen
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/
Author
11 Mar 2006 9:44 PM
David Thielen
perfect - thanks

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



Show quote
"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/
>

AddThis Social Bookmark Button