Hi All !
Here is how u can validate an xml with an xsd which is dynamically attached
to it...
For this we use the classes in the namespaces System.Xml amd
System.Xml.Schema.
Specifically we use the XmlValidatingReader class.
1. First read your xml file into an XmlTextReader (say into a variable names
objXmlReader).
2. Read the xsd contents into an XmlTextReader (say into a varibale named
objXsdReader).
3. Add the xsd to an instance of XmlSchemaCollection;
XmlSchemaCollection objSchemaCol = new XmlSchemaCollection();
objSchemaCol.Add(null, objXsdReader);
4. Instantiate an XmlValidatingReader as shown below.
XmlValidatingReader objValidator = new XmlValidatingReader(objXmlReader);
//set the validation type
objValidator.ValidationType = ValidationType.Auto;
//Add the schema collection against which u wanna validate
if(objSchemaCol != null)
{
objValidator.Schemas.Add(objSchemaCol);
}
5. Attach the event handler to catch schema errors
objValidator.ValidationEventHandler += new
ValidationEventHandler(this.Validation_Callback);
while(objValidator.Read())
{
//do nothing
}
6. In the event handler
private void Validation_Callback(object sender, ValidationEventArgs args)
{
//I have used a class member level variable
m_sSchemaErrors to concatenate the schema errors which can
//later be displayed to the user.
//If u declare objXmlReader as a member level
variable in the class, u can capture the line no and column of the error.
this.m_sSchemaErrors += args.Message + " Line: "+
objXmlReader.LineNumber + " Column: " + objXmlReader.LinePosition + "\n";
}
Thus u can achieve the validation.
Cheers,
Deepa.
-----Original Message-----
From: Sphinx [mailto:[EMAIL PROTECTED]
Sent: Friday, March 28, 2003 4:42 AM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Validating a xml file against a dtd when
no dtd reference is actually in the xml document
Hai Joel Mueller,
Thanks for ur reply.
Can u please tell how to dynamically validate a xml file against xsd when
there is no reference to xsd in the xml file.
Chito.
----- Original Message -----
From: "Joel Mueller" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, March 26, 2003 10:44 AM
Subject: Re: [ADVANCED-DOTNET] Validating a xml file against a dtd when no
dtd reference is actually in the xml document
> You might want to consider converting your DTD to an XSD. The XSD
> support in .NET is much better, and it's not hard at all to do what you
> want with an XSD.
>
> > -----Original Message-----
> > From: Moderated discussion of advanced .NET topics.
> > [mailto:[EMAIL PROTECTED] On Behalf Of Sphinx
> > Sent: Wednesday, March 26, 2003 8:43 PM
> > To: [EMAIL PROTECTED]
> > Subject: [ADVANCED-DOTNET] Validating a xml file against a
> > dtd when no dtd reference is actually in the xml document
> >
> >
> > Hello,
> >
> > How to validate a xml document against a dtd when there is no
> > dtd reference in the xml document. I have used
> > XMLParserContext and XmlValidatingReader classes. But, it is
> > not working. I used the following code..
> >
> > //PEdConfig is the name of the dtd
> >
> > XmlParserContext context = new XmlParserContext(null, null,
> > "PEdConfig", null, null, null, "", "", XmlSpace.None);
> >
> > //strFileName contains name of the xml file
> >
> > FileStream filestreamSource = new FileStream(strFileName,
> > FileMode.Open, FileAccess.Read);
> >
> >
> > XmlValidatingReader reader = new
> > XmlValidatingReader(filestreamSource, XmlNodeType.Element, context);
> >
> > reader.ValidationType = ValidationType.DTD;
> >
> >
> > while(reader.Read())
> >
> > {
> >
> > }
> >
> > Thanks ,
> >
> > Chito.
> >
> >
>