XML data validation : Validate XML « XML « ASP.Net

XML data validation

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Schema" %>

<script runat="server">    
    private StringBuilder stringBuilder = new StringBuilder();
    void Page_Load(object sender, EventArgs e)
        string xmlPath = MapPath("Authors.xml");    
        string xsdPath = MapPath("Authors.xsd");

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
        settings.Schemas.Add(null, XmlReader.Create(xsdPath));
        XmlReader reader = XmlReader.Create(xmlPath, settings);          
        XmlDocument doc = new XmlDocument();          
        ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
        if (stringBuilder.ToString() == String.Empty)
            Response.Write("Validation completed successfully.");
            Response.Write("Validation Failed. <br>" + stringBuilder.ToString());

    void ValidationEventHandler(object sender, ValidationEventArgs args)
        stringBuilder.Append("Validation error: " + args.Message + "<br>");                
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>XSD Validation</title>
    <form id="form1" runat="server">


Related examples in the same category

1.Use XML Document (DOM) to validate XML against XML schema
2.Validate XML against XML schema