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
Step-1
open .net Visual studio 2005 or latter create new project select window project and language is c#
Step-2
Following name space will added
using System.Xml; using System.Xml.Schema;Step-3
Take one window form and put one Textbox, open Button(for Open file Dialog box), validate button and fileopenDialogbox place on form.like
when we click open button one dialog box open and take xml path for validate xml
Step-4
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
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); }Put Following code in Validate buttion Click event
private void btnValidate_Click(object sender, EventArgs e) { ValiDateXML(Convert.ToString(Txtoutputpath.Text)); }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.
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); } }Conclusion
I have tried best to explain how validate bunch of xml file which have same DTD(Document Type Definition)
Happy Codding
Thank you