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 
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
|
|
|