|
HTML clipboard
About XmlSchema Class
Contains the definition of a schema. All XML Schema definition language (XSD)
elements are children of the schema
ReadTargetNamespaceTargetNamepace is used to Get or
set the property for the schema target namespace. The URI reference of the
namespace of this schema. We use Method Read to be able to get the
TargetNamepace property, or Get an XmlSchema reference to be added to
XmlSchemaCollection, you'll see how later on this tutroial
Example 2:
element. Represents the World Wide
Web Consortium (W3C) schema element. In this Class we will use the Method and the Property
new ValidationEventHandler(SchemaReadError));
XmlSchema xmlSchema = XmlSchema.Read(xReader,
Note: XmlSchema Class can be used for XSD Only not XDR.
About XmlSchemaCollection:
From its name, it is a Colletion of Schemas that will be used to validate the
XML Documents, this class has Add Method which will be used to add the
schemas to collection, this Method is over loaded, in this tutorial we are using
2 of those overloaded, we'll see that later.
Example 3:
XmlSchemaCollection m_schSchemas = new XmlSchemaCollection();
m_schSchemas.Add(xmlSchema);
string namespace,
string URI_or_filePath);
m_schSchemas.Add(
About XmlValidatingReader:
Represents a reader that provides DTD, XML-Data Reduced (XDR) schema, and XML
Schema definition language (XSD) schema validation.
This is used to validate the XML Document, we will perform the validation node
by node according to the defined schema in the XML Document. The Constructor of
this Class takes XmlReader as parameter so we'll pass the XmlTextReader object
to this constructor. This Class also has Schemas Property which is used
to assign the XmlSchemaCollection to use for validation.
Also there is a property called ValidationType it is an enum type which
has 5 posible values Auto,None,Schema,DTD & XDR, I'll use Auto which will make
XmlValidatingReader validates if DTD or schema information is found. This
property must be set before the first call to Read Method or
InvalidOperationException will be thrown.
Also don't forget to set the ValidationEventHandler event of the
XmlValidatingReader object.
Program describtion:
First we will load the XML document we want to work on in a combo box, and the
Validation documents will be loaded in an XmlSchemaCollection.
we choose the XML document from the Combo box then click on validate and the
document will be validated Automaticly as we set the ValidationType
property to Auto, the results will be displayed in a text box.
Code:
private string[] m_strXmlFiles;
private XmlSchemaCollection m_schSchemas;
private bool m_bValid;
public frmXmlValidation()
{
InitializeComponent();
m_schSchemas = new
XmlSchemaCollection();
m_bValid = true;
}
private void btnLoadXml_Click(object
sender, System.EventArgs e)
{
dlgFileChooser.Filter = "Xml Files | *.xml";
dlgFileChooser.Multiselect = true;
dlgFileChooser.Title = "Load Xml Files";
DialogResult Results = dlgFileChooser.ShowDialog();
if(Results == DialogResult.Cancel)
return;
cmbXmlFiles.Items.Clear();
m_strXmlFiles = dlgFileChooser.FileNames;
string[] FileNames = dlgFileChooser.FileNames;
for(int i=0;
i{
string
strItem = dlgFileChooser.FileNames[i];
int intIndex
= strItem.LastIndexOf("\\");
FileNames[i] =
strItem.Substring(intIndex+1);
}
cmbXmlFiles.Items.AddRange(FileNames);
}
private void btnLoadValidationFiles_Click(object
sender, System.EventArgs e)
{
dlgFileChooser.Filter = "XDR Files | *.xdr| XSD Files | *.xsd|Both
|*.xsd;*.xdr";
dlgFileChooser.Multiselect = true;
dlgFileChooser.Title = "Load Xml Validation Files [XDR & XSD
Files]";
dlgFileChooser.FilterIndex = 3;
DialogResult Results =
dlgFileChooser.ShowDialog();
if(Results == DialogResult.Cancel)
return;
for(int i=0;
i{
int len =
dlgFileChooser.FileNames[i].Length;
if(dlgFileChooser.FileNames[i].Substring(len-3).Equals("xsd"))
{
XmlTextReader
xReader = new XmlTextReader(dlgFileChooser.FileNames[i]);
try
{
XmlSchema xmlSchema = XmlSchema.Read(xReader,
new ValidationEventHandler(SchemaReadError));
m_schSchemas.Add(xmlSchema);
}
catch(XmlException err)
{
MessageBox.Show(err.Message+ " in " + Environment.NewLine +
dlgFileChooser.FileNames[i],
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
if(xReader != null)
xReader.Close();
}
}
else
{
string strFileName = dlgFileChooser.FileNames[i];
int intSlashIndex = strFileName.LastIndexOf('\\');
strFileName =
CString.Right(strFileName, len - intSlashIndex-1);
m_schSchemas.Add(strFileName,dlgFileChooser.FileNames[i]);
}
}
}
private void btnValidate_Click(object
sender, System.EventArgs e)
{
int intFileIndex =
cmbXmlFiles.SelectedIndex;
if(intFileIndex == -1)
{
MessageBox.Show("Please Load XML
Files","Note",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return;
}
if(m_schSchemas.Count == 0)
{
MessageBox.Show("Please Load
Validation Files",
"Note",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return;
}
XmlTextReader xReader;
string strFileName;
strFileName = m_strXmlFiles[intFileIndex];
xReader = new
XmlTextReader(strFileName);
XmlValidatingReader xValidator = new
XmlValidatingReader(xReader);
xValidator.Schemas.Add(m_schSchemas);
xValidator.ValidationType = ValidationType.Auto;
xValidator.ValidationEventHandler += new
ValidationEventHandler(ValidationError);
while(xValidator.Read()) ;
if(m_bValid)
txtOutput.Text =cmbXmlFiles.SelectedItem.ToString()
+ " is valid document";
m_bValid = true;
xValidator.Close();
}
private void ValidationError(object
sender, ValidationEventArgs arguments)
{
txtOutput.Text = arguments.Message;
m_bValid = false;
}
|