code_junkie

Easiest way to validate and read an xml file in .net?

Nov 13th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. StreamReader xsdReader = new StreamReader(xsdFileName);
  2. XmlSchema Schema = new XmlSchema();
  3. Schema = XmlSchema.Read(xsdReader, new ValidationEventHandler(XSDValidationEventHandler));
  4.  
  5. private static void XSDValidationEventHandler(object sender, ValidationEventArgs e)
  6.  
  7. XmlReaderSettings ReaderSettings = new XmlReaderSettings();
  8. ReaderSettings.ValidationType = ValidationType.Schema;
  9. ReaderSettings.Schemas.Add(Schema);
  10. ReaderSettings.ValidationEventHandler += new ValidationEventHandler(XMLValidationEventHandler);
  11.  
  12. XmlTextReader xmlReader = new XmlTextReader(xmlFileName);
  13. XmlReader objXmlReader = XmlReader.Create(xmlReader, ReaderSettings);
  14. while (objXmlReader.Read()) { }
Add Comment
Please, Sign In to add comment