XML Validation with DTD : DTD « XML « ASP.Net






XML Validation with DTD

<%@ 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("AuthorsWithDTD.xml");            
        XmlReader reader = null;        
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationEventHandler += new ValidationEventHandler(this.ValidationEventHandler);
        settings.ValidationType = ValidationType.DTD;                
        settings.ProhibitDtd = false;
        reader = XmlReader.Create(xmlPath, settings);
        while (reader.Read()) 
        {             
        }
        if (stringBuilder.ToString() == String.Empty)
            Response.Write("DTD Validation completed successfully.");
        else
            Response.Write("DTD Validation Failed. <br>" + stringBuilder.ToString());
    }

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


<%-- AuthorsWithDTD.xml
<?xml version="1.0"?>
<!DOCTYPE authors SYSTEM "Authors.dtd">
<authors>
  <author>
    <au_id>000-00-0001</au_id>
    <au_lname>Nancy</au_lname>
    <au_fname>Lee</au_fname>
    <phone>999 999-9999</phone>
    <address>9999 York St.</address>
    <city>Regina</city>
    <state>LA</state>
    <zip>99999</zip>
    <contract>true</contract>
  </author>
  <author>
    <au_id>000-00-0002</au_id>
    <au_lname>First</au_lname>
    <au_fname>Last</au_fname>
    <phone>415 986-7020</phone>
    <address>No Name St.</address>
    <city>Vancouver</city>
    <state>BC</state>
    <zip>88888</zip>
    <contract>true</contract>
  </author> 
</authors>


--%>


<%-- Authors.dtd
<?xml version="1.0"?>
<authors>
  <author>
    <authorID>000-00-0001</authorID>
    <lastName>Nancy</lastName>
    <firstName>Lee</firstName>
    <phone>999 999-9999</phone>
    <address>9999 York St.</address>
    <city>Regina</city>
    <state>LA</state>
    <zip>99999</zip>
    <contract>true</contract>
  </author>
  <author>
    <authorID>000-00-0002</authorID>
    <lastName>First</lastName>
    <firstName>Last</firstName>
    <phone>415 986-7020</phone>
    <address>No Name St.</address>
    <city>Vancouver</city>
    <state>BC</state>
    <zip>88888</zip>
    <contract>true</contract>
  </author> 
</authors>

--%>
           
       








Related examples in the same category