Introduction This article explains about how to validate an XML document with XSD schema. The validation is performed by checking whether the XML document is a well-formed one by programmatically using .NET classes in C#. XML document An XML document contains elements, attributes, and values of primitive data types. For example, consider the following XML […]
CodeGuru content and product recommendations are
editorially independent. We may make money when you click on links
to our partners.
Learn More
Introduction
This article explains about how to validate an XML document with XSD schema. The validation is performed by checking whether the XML document is a well-formed one by programmatically using .NET classes in C#.
XML document
An XML document contains elements, attributes, and values of primitive data types. For example, consider the following XML document:
XSD schema defines elements, attributes, and the relationship between them. It conforms to the W3C XML schema standards and recommendations. XSD schema for the above XML document, address.xsd, can be given as follows:
The ValidationEventHandler event is used to define an event handler for receiving the notification about XSD schema validation errors. The validation errors and warnings are reported through the ValidationEventHandler call-back function. If any validation error occurs, an exception is thrown, and the XmlValidatingReader cannot be restarted.
XML validation with .NET classes
Microsoft .NET framework classes support the W3C XML schema recommendation. The classes used to validate the XML document are XmlTextReader, XmlSchemaCollection, and XmlValidatingReader. The sequence of steps to validate an XML document is given as follows:
The ValidationHandler event handler method is defined.
The XSD schema is being parsed using XmlTextReader class.
The parsed schema is added to the schema collection using XmlSchemaCollection class.
The schema collection is associated with XmlValidatingReader class.
The event handler method is associated with XmlValidatingReader class.
XmlValidatingReader class validates the XML document using the namespace URI specified when the schema was added to the collection.
Advertisement
Sample Code
The following example shows how to validate XML document against XSD schema by using Microsoft .NET framework classes.
CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.
Advertiser Disclosure: Some of the products that appear on
this site are from companies from which TechnologyAdvice
receives compensation. This compensation may impact how and
where products appear on this site including, for example,
the order in which they appear. TechnologyAdvice does not
include all companies or all types of products available in
the marketplace.