Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   |   Class Browser
  |   I want my samples in...   

How Do I...? Common Tasks QuickStart Tutorial

Go To...

How Do I...Validate an XML Document?

This sample illustrates how to validate an XML document by 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.

C# XmlDocumentValidation.exe
View Source

[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\Xml\XmlDocumentValidation\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin directory.]

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 � 2005 Microsoft Corporation. All rights reserved.


Hosted by MaximumASP | Found a broken link? | Contact Us | Terms and conditions | Privacy Policy | Advertise with us
� 2000 - 2009  Mindcracker LLC. All Rights Reserved