Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Validating an XML Document using DTD in ASP.NET
 

Validating an XML Document using DTD in ASP.NET

by Faisal Khan.Follow Faisal Khan on Twitter

Introduction
In this tutorial, we will learn how to validate an XML document using DTD. We will develop an ASP.NET page to demonstrate the code. After you are finished reading this tutorial, you will be proficient in writing C# code that validates XML documents using DTD.

Following topics will be covered in this tutorial:

This tutorial is 6th one in the series of tutorials about XML and ASP.NET:

We will first refresh ourselves with basic XML concepts of well-formedness and validation, and later develop the code to validate XML document using DTD.

Well-formedness vs Validation
Well-formedness means that an XML document obeys the rules that are necessary for an XML document to be, frankly, an XML document. What this means is that there should be an XML declaration, there should be a root element, the name of element in opening tag must match with the name in the closing tag, and so on. We covered a good part of it in the first article on introduction to XML.

Validation on the other hand is an entirely different thing. An XML document is valid only if it conforms to the constraints declared in DTD. Put it simply, if the structure of an XML document is exactly as it is described in DTD, then that XML document is valid (in reference to that DTD), otherwise it is not.

Validating XML Document using DTD
We will use the sample XML document and DTD file from the article on Document Type Definition. We will then write code to validate that XML document using that DTD.

Sample XML Document
Following is the XML document that we will be using for demonstration:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE article SYSTEM "article.dtd">

<article>
	<title>Sample XML Document.</title>
	<author email="hidden@xyz.com">Faisal Khan</author>
	<body>This is a sample XML Document.</body>
</article>

Copy above code in a new text file and save it as "article.xml" in the /App_Data folder of your ASP.NET application.

Sample DTD File
Following is the DTD file that we will be using for demonstration:

<!ELEMENT article (title, author?, body)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)> 
<!ELEMENT body (#PCDATA)>

<!ATTLIST author email CDATA #REQUIRED>

Copy above code in a new text file and save it as "article.dtd" in the /App_Data folder of your ASP.NET application.

If you look at the XML document and DTD file closely, you'll see that the XML document conforms to the constraints in the DTD file, and is thus, a valid XML document with respect to that DTD file. To do this manual verfication programmatically, we'll have to write code. Luckily, in .NET, writing code that validates an XML document is quite simple.

Introduction
In this tutorial, we will learn how to validate an XML document using DTD. We will develop an ASP.NET page to demonstrate the code. After you are finished reading this tutorial, you will be proficient in writing C# code that validates XML documents using DTD.

Following topics will be covered in this tutorial:

This tutorial is 6th one in the series of tutorials about XML and ASP.NET:

We will first refresh ourselves with basic XML concepts of well-formedness and validation, and later develop the code to validate XML document using DTD.

Well-formedness vs Validation
Well-formedness means that an XML document obeys the rules that are necessary for an XML document to be, frankly, an XML document. What this means is that there should be an XML declaration, there should be a root element, the name of element in opening tag must match with the name in the closing tag, and so on. We covered a good part of it in the first article on introduction to XML.

Validation on the other hand is an entirely different thing. An XML document is valid only if it conforms to the constraints declared in DTD. Put it simply, if the structure of an XML document is exactly as it is described in DTD, then that XML document is valid (in reference to that DTD), otherwise it is not.

Validating XML Document using DTD
We will use the sample XML document and DTD file from the article on Document Type Definition. We will then write code to validate that XML document using that DTD.

Sample XML Document
Following is the XML document that we will be using for demonstration:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE article SYSTEM "article.dtd">

<article>
	<title>Sample XML Document.</title>
	<author email="hidden@xyz.com">Faisal Khan</author>
	<body>This is a sample XML Document.</body>
</article>

Copy above code in a new text file and save it as "article.xml" in the /App_Data folder of your ASP.NET application.

Sample DTD File
Following is the DTD file that we will be using for demonstration:

<!ELEMENT article (title, author?, body)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)> 
<!ELEMENT body (#PCDATA)>

<!ATTLIST author email CDATA #REQUIRED>

Copy above code in a new text file and save it as "article.dtd" in the /App_Data folder of your ASP.NET application.

If you look at the XML document and DTD file closely, you'll see that the XML document conforms to the constraints in the DTD file, and is thus, a valid XML document with respect to that DTD file. To do this manual verfication programmatically, we'll have to write code. Luckily, in .NET, writing code that validates an XML document is quite simple.


 ( 2 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions

No Comments Found.


Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.