13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}if (e.Severity == XmlSeverityType.Error)m_isValid = false;// Validate the specified <strong>XML</strong> document (us<strong>in</strong>g Auto mode)public static bool ValidateXmlDocument(str<strong>in</strong>g fileName){XmlTextReader _coreReader = new XmlTextReader(fileName);XmlValidat<strong>in</strong>gReader reader = newXmlValidat<strong>in</strong>gReader(_coreReader);reader.ValidationType = ValidationType.Auto;reader.ValidationEventHandler +=new ValidationEventHandler(XmlValidator.ErrorHandler);// Parse the documenttry{m_isValid = true;while(reader.Read() && m_isValid) {}}catch{m_isValid = false;}}}reader.Close();return m_isValid;The ValidateXmlDocument method loops through the nodes of the document until the<strong>in</strong>ternal member m_isValid is false or the end of the stream is reached. The m_isValidmember is set to true at the beg<strong>in</strong>n<strong>in</strong>g of the loop and changes to false the first time anerror is found. At this po<strong>in</strong>t, the document is certa<strong>in</strong>ly <strong>in</strong>valid, so there is no reason tocont<strong>in</strong>ue loop<strong>in</strong>g.Because the ValidateXmlDocument method is declared static (or Shared <strong>in</strong> <strong>Microsoft</strong>Visual Basic .<strong>NET</strong>), you don't need a particular <strong>in</strong>stance of the base class to issue thecall, as shown here:if(!XmlValidator.ValidateXmlDocument("data.xml"))MessageBox.Show("Not a valid document!");NoteThe reader's <strong>in</strong>ternal mechanisms responsible <strong>for</strong> check<strong>in</strong>g adocument's well-<strong>for</strong>medness and schema compliance are dist<strong>in</strong>ct.So if a validat<strong>in</strong>g reader happens to work on a badly <strong>for</strong>med <strong>XML</strong>68

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!