XML validation: Difference between revisions
Spintronic (talk | contribs) (Created page with "{{draft task}} Given an XML document and an XSD schema definition validate that the document follows the schema described. =={{header|C sharp}}== <lang csharp> using System...") |
Spintronic (talk | contribs) |
||
Line 16: | Line 16: | ||
// your code goes here |
// your code goes here |
||
XmlSchemaSet sc = new XmlSchemaSet(); |
XmlSchemaSet sc = new XmlSchemaSet(); |
||
sc.Add(null, "http:// |
sc.Add(null, "http://venus.eas.asu.edu/WSRepository/xml/Courses.xsd"); |
||
XmlReaderSettings settings = new XmlReaderSettings(); |
XmlReaderSettings settings = new XmlReaderSettings(); |
||
settings.ValidationType = ValidationType.Schema; |
settings.ValidationType = ValidationType.Schema; |
||
Line 22: | Line 22: | ||
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); |
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); |
||
// Create the XmlReader object. |
// Create the XmlReader object. |
||
XmlReader reader = XmlReader.Create("http:// |
XmlReader reader = XmlReader.Create("http://venus.eas.asu.edu/WSRepository/xml/Courses.xml", settings); |
||
// Parse the file. |
// Parse the file. |
||
while (reader.Read()); |
while (reader.Read()); |
Revision as of 23:35, 3 November 2014
Given an XML document and an XSD schema definition validate that the document follows the schema described.
C#
<lang csharp> using System; using System.Xml; using System.Xml.Schema; using System.IO;
public class Test { public static void Main() { // your code goes here XmlSchemaSet sc = new XmlSchemaSet(); sc.Add(null, "http://venus.eas.asu.edu/WSRepository/xml/Courses.xsd"); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas = sc; settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); // Create the XmlReader object. XmlReader reader = XmlReader.Create("http://venus.eas.asu.edu/WSRepository/xml/Courses.xml", settings); // Parse the file. while (reader.Read()); // will call event handler if invalid Console.WriteLine("The XML file is valid for the given xsd file"); }
// Display any validation errors. private static void ValidationCallBack(object sender, ValidationEventArgs e) { Console.WriteLine("Validation Error: {0}", e.Message); } } </lang>