using System; using System.Data; using System.Xml; // for XmlTextReader and XmlValidatingReader using System.Xml.Schema; // for XmlSchemaCollection (which is used later) using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { private static bool isValid = true; private static string error = ""; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (xmlFileName.Text == "") { txtTulemus.Text = "Error ............. XML fail määramata!"; } else { txtTulemus.Text = ""; XmlReader xr = null; isValid = true; XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationEventHandler +=new ValidationEventHandler(settings_ValidationEventHandler); settings.ProhibitDtd = false; settings.ValidationType = ValidationType.DTD; txtTulemus.Text += "Kontrollin vastavust DTD'le"; xr = XmlReader.Create(xmlFileName.Text, settings); while (xr.Read()) { } // Check whether the document is valid or invalid. if (isValid) txtTulemus.Text += " .............. OK" + Environment.NewLine; else { txtTulemus.Text += Environment.NewLine + error; error = ""; isValid = true; } settings.ValidationType = ValidationType.Schema; settings.ValidationFlags = XmlSchemaValidationFlags.ProcessSchemaLocation; xr = XmlReader.Create(xmlFileName.Text, settings); txtTulemus.Text += Environment.NewLine + "Kontrollin vastavust Schema'le"; while (xr.Read()) { } // Check whether the document is valid or invalid. if (isValid) txtTulemus.Text += " .............. OK" + Environment.NewLine; else { txtTulemus.Text += Environment.NewLine + error; error = ""; isValid = true; } } } void settings_ValidationEventHandler(object sender, ValidationEventArgs e) { isValid = false; error += "Error real " + e.Exception.LineNumber + ": " + e.Message + Environment.NewLine; } private void button2_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "XML Failid|*.xml"; ofd.ShowDialog(); if (ofd.FileName != "") xmlFileName.Text = ofd.FileName; } } }