Validate XML with XSD

15. May 2006

To validate an XML file with an XSD file, use this !

public static XmlDocument ValidateAndLoadXmlDocument(string xmlFilePath, string xsdFilePath)
{

XmlDocument xmlDoc;

XmlReaderSettings xmlSettings = new XmlReaderSettings();
xmlSettings.Schemas.Add(null, xsdFilePath);
xmlSettings.ValidationType = ValidationType.Schema;

XmlReader xmlReader = XmlReader.Create(xmlFilePath, xmlSettings);
xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);

return xmlDoc;

}

Use this for asynchronous validation :

xmlSettings.ValidationEventHandler += new ValidationEventHandler(YourValidationEventHandler);

 

XML, Web Services, .NET Projects , ,

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading