|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Verify an xml file is validHi;
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.) David Thielen wrote:
> Hi; Just run a parser/validator over it.> > 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.) See the FAQ: http://xml.silmaril.ie/authors/parsers/ ///Peter 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. 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 > 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." Kevin - that is way too easy <g>.
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." > > 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." >> >> David Thielen wrote:
> What's the fastest way to verify that an xml file is well-formed (and valid With .NET 1.x you should use an XmlValidatingReader then (if you want to > 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. 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. 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. 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/ > David Thielen wrote:
> Yes it is .NET 2.0. And I realized last night that XmlDocument is creating a ..NET has no SAX push parser but has a pull parser with XmlReader, here > 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. 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> perfect - thanks
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/ > |
|||||||||||||||||||||||