Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validating xml against schema

Status
Not open for further replies.

5679

Programmer
Feb 14, 2003
32
AU
I have an xml file and a schema.I have to validate the xml file against the schema. How can i do this in c#?
 
Heres a solution validating against a dtd. Should be easy to change to schema.

using System.Xml;
using System.Xml.Schema;

public static void handle_validation(object s, ValidationEventArgs a)

{
throw new Exception("The file could not be validatet " + a.Message);
}


try
{
XmlTextReader r = new XmlTextReader(path); //insert path to xml file here
XmlValidatingReader v = new XmlValidatingReader(r); // new Instance of validating reader
v.ValidationType = ValidationType.DTD; // setting the type of the scheme (DTD in this case)
v.ValidationEventHandler += new ValidationEventHandler (handle_validation); //new Eventhandler for the validation
while(v.Read());
v.Close();
r.Close();
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top