Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Validate an XML Document?

This sample illustrates how to validate an XML document using a validating XML reader, and the Validate method of the XmlDocument class. A validating XML reader is used to create the XmlDocument object that contains the XML document to validate. The XmlReaderSettings object used to create the validating XmlReader object contains the schema used to validate the XML document, and specifies the ValidationEventHandler used to handle schema validation warnings and errors.

VB XmlDocumentValidation.exe
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\Xml\XmlDocumentValidation\]

The following code creates the XmlReaderSettings object.

		
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("urn:xmlns:25hoursaday-com:my-bookshelf", xsd);
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
settings.ValidationType = ValidationType.Schema;
C#

The following code creates the validating XmlReader object and the XmlDocument object.

		
XmlDocument doc = new XmlDocument();
doc.Load(XmlReader.Create(document, settings));
C#

The following code validates the changes made to the XML document.

		
doc.Validate(new ValidationEventHandler(ValidationCallback), element.ParentNode);
C#



Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.