i wrote a program sometimes to validate my xml file against the schema using c#.net but i found out that whether there was error or no error, it will always say the document is valid even if i specify the wrong file name. i want you to give me a snippet of code and the xml file to see how to validate in C#.net
validating an xml document.
Collapse
X
-
Tags: None
-
protected void Page_Load(objec t sender, EventArgs e)
{
try
{
string a,b,error;
strXml = "Example.xm l";
strXsd = "Example.xs d";
XmlSchemaCollec tion sc = new XmlSchemaCollec tion();
sc.ValidationEv entHandler += new ValidationEvent Handler(Validat ionCallBack);
sc.Add(null, strXsd);
//XmlTextReader textReader = new XmlTextReader(s trXml);
//XmlValidatingRe ader validatingReade r = new XmlValidatingRe ader(textReader );
//validatingReade r.ValidationTyp e = ValidationType. Schema;
XmlSchema schema = XmlSchema.Read( new XmlTextReader(s trXsd), null);
schema.SourceUr i = strXsd;
XmlValidatingRe ader validatingReade r = new XmlValidatingRe ader(new
XmlTextReader(s trXml));
validatingReade r.Schemas.Add(s chema);
validatingReade r.ValidationEve ntHandler += new ValidationEvent Handler(Validat ionCallBack);
while (validatingRead er.Read()) ;
a = "Success";
error="Error";
if(error==a)
{
Response.Write( "Invalid File "+b);
}
else
{
Response.write( "Xml IS valid");
}
}
catch(Exception ex)
{
a = "Error";
b = exc.Message;
}
}
private void ValidationCallB ack(object sender,System.X ml.Schema.Valid ationEventArgs e)
{
if (e.Message != "")
{
a = "Error";
b = e.Message;
}
}
I hope this will help you.
Comment