Validate XML over DTD

No.of Views1116
Bookmarked0 times
Downloads 
Votes1
By  Kirti.M.Darji   On  15 Feb 2010 22:02:04
Tag : XML , How to
Validate XML over DTD
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

We can Easily Export data from Excel, CSV file into individual Xml per Record. There is multiple xml file generated per record. This article explains how to validate xml over DTD (Document Type Definition).

Technologies

.NET

Language

C#

Prerequisite

Visual Studio 2005 and Later

Implementation

Following namespace will added

using System.Xml;
using System.Xml.Schema;

Take one window for in put one Textbox, open Button, validate button and fileopenDialogbox place on form.like

Open dialog

Which Take folder path of multiple xml files

Define Following Variable Global in form Class

private static bool isValid = true;
String StrErrorPath = Application.StartupPath + "/ValidateError.log";

Put Following code in open button click Event

{codecitation class="brush: csharp; gutter: true;" width="600px"}

private void btnoutput_Click(object sender, EventArgs e)
{

String strFolderName;
FDBrowserDL.ShowNewFolderButton = true;
FDBrowserDL.ShowDialog();
strFolderName = FDBrowserDL.SelectedPath;
Txtoutputpath.Text = strFolderName;

Txtoutputpath.Text = Convert.ToString(strFolderName);
}

{/codecitation}
Put Following code in Validate buttion Click event


{codecitation class="brush: csharp; gutter: true;" width="600px"}

private void btnValidate_Click(object sender, EventArgs e)
{

ValiDateXML(Convert.ToString(Txtoutputpath.Text));

}

{/codecitation}

Following are function Validate Xml over DTD. If there is error in file means if Xml file not match over DTD then it Give Error and Write Error in Error file with Description.


{codecitation class="brush: csharp; gutter: true;" width="600px"}

private void ValiDateXML(String StrFilePath)
{
Int64 i=0;
String[] fileEntries = Directory.GetFiles(StrFilePath);
foreach (String Sfile in fileEntries)
{
Application.DoEvents();
XmlTextReader r = new XmlTextReader(Sfile);
r.WhitespaceHandling = WhitespaceHandling.None;
XmlValidatingReader v = new XmlValidatingReader(r);
v.ValidationType = ValidationType.DTD;

v.ValidationEventHandler += MyValidationEventHandler;


i = i + 1;
Resume:
try
{
while (v.Read())

{
}
lblMessage.Text = "Validating File : " + i.ToString();
}
catch (Exception Ex)
{

WriteError(Sfile, Ex.Message);
goto Resume;
}

v.Close();



}
lblMessage.Text = "Check Validation Complete";
}



public void MyValidationEventHandler(object sender,
ValidationEventArgs args)
{
isValid = false;

WriteError(sender.ToString(),args.Message);

}


private void WriteError( String StrFilename, String Ex)
{
try
StreamWriter sw;

if (File.Exists(StrErrorPath)==false)
{
sw = new StreamWriter(StrErrorPath);
}
else
{
sw = new StreamWriter(StrErrorPath, true);
}

sw.WriteLine(StrFilename + " " +Ex);

sw.WriteLine("From the StreamWriter class");
sw.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error occurred at writting to Error Log file. -- " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
{/codecitation}


Conclusion

This article explain how to validate xml file with DTD (Document Type Definition)

Thank you


About the Author


Kirti Darji
P.G.D.C.A, M.SC(Computer Science) 3+ YEAR EXPERIENCE Visual C#.net and VB.net Windows and Web base Application

Expertise :C#.net, VB.Net,ASP.net,SQL Server, MS Access,aJAX, JavaScript,CSS,HTML,XML,N TIRE ARCHITECTURE,oOPS,Web Services,Core Ajax,visual Source safe,IIS

Occupation :SR. Software Developer
Company : MADHUVAN INFO TECH PVT.LTD
Location :AHMADABAD


 
Sign Up to vote for this article
 
About Author
 
Kirti.M.Darji
Occupation-Software Engineer
Company-Maven-Infosoft Pvt.Ltd
Member Type-Expert
Location-Not Provided
Joined date-03 Jun 2009
Home Page-
Blog Page-http://kirtimdarji.blogspot.com/
I am Kirti M. Darji Senior Software Developer Having 3.5 Years Experience In Microsoft .Net(c#) Technologies.
 
 
Other popularSectionarticles
    Many times we have a scenario like we need to use some hardcode values like our SQL connection string or some List name or any configuration settings like network credentials.
    Published Date : 11/Jun/2010
    This writing does not include a full discussion or even the full details of RSS or XML. Rather, it includes a nice introduction to RSS and its XML schema. In addition, it incorporates what you get in a sample application that is easy-to-code, understand, and to extend.
    Published Date : 10/Sep/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top